PDA

View Full Version : strcmp function ! help me!


superrobot48
06/05/2014, 09:36 AM
i read the tutorials
THis (http://wiki.sa-mp.com/wiki/Strcmp)
and i cant get this function to work
. this function is to check how many cars the player have.


GetPlayerVehicles(playerid)
{
new playername[24];
GetPlayerName(playerid, playername, sizeof(playername));
new count;
for(new i = 0; i < MAX_VEHICLES; i++) // For every vehicles till the maximum amount
{
if((vCreated[i] == true) && strcmp(VehicleInfo[i][vOwner],playername == 0))
{
count++;
}
}
return count;
}


enum where vOwner is defined

enum vInfo
{
vID, // The vehicle id used on the SA-MP to represent the vehicle.
vModel, // The vehicle model of the vehicle.
Float:xspawn,
Float:yspawn,
Float:zspawn,
Float:anglespawn, // The array which contains position and the rotation of the vehicle in 4 cells. So, basically {X = 0, Y = 1, Z = 2, R = 3}
vColor1, // The primary color of the vehicle.
vColor2, // The secondary color of the vehicle.
vOwner[MAX_PLAYER_NAME],
bombed, // The vehicle's owner name. Remember string is a group of literal characters and the cells you provide should be the maximum length for that string.
bool:vLocked // Bool representing if the vehicle is locked or not. (0 = False, 1 = True)
}
new VehicleInfo[MAX_VEHICLES][vInfo];

the error:

E:\samp03x_svr_R2_win32\gamemodes\Untitled.pwn(463 6) : error 033: array must be indexed (variable "playername")
Pawn compiler 3.2.3664 Copyright (c) 1997-2006, ITB CompuPhase


1 Error.

PLEASE HELP GUYZ!

Konstantinos
06/05/2014, 09:42 AM
You first need to close with ")" so strcmp will return a value and then compare if it's equal to 0.

strcmp(VehicleInfo[i][vOwner],playername) == 0


Note that if VehicleInfo[i][vOwner] is null/empty, it will return false. You can use isnull macro and check if it's not null and then compare to the player's name.

You don't need an array so you know if a vehicle is created or not. If the vehicle doesn't exist, GetVehicleModel returns 0 and there's a native as well which called IsValidVehicle (not defined so you should add it yourself).

superrobot48
06/05/2014, 10:15 AM
Thanks :D

"Note that if VehicleInfo[i][vOwner] is null/empty, it will return false. You can use isnull macro and check if it's not null and then compare to the player's name.

You don't need an array so you know if a vehicle is created or not. If the vehicle doesn't exist, GetVehicleModel returns 0 and there's a native as well which called IsValidVehicle (not defined so you should add it yourself)."
i added vCreated[i] == true to check if vehicle is created ;)

superrobot48
06/05/2014, 11:17 AM
And Why doesnt this work ?


if(newstate == PLAYER_STATE_DRIVER)
{
if((vCreated[vehicleid] == true) && strcmp(VehicleInfo[vehicleid][vOwner],PlayerName(playerid)) == 0)
{
SendClientMessage(playerid,COLOR_GREEN,"Welcome to your purchased vehicle");
return 1;
}
}

i added this on OnPlayerStateChange
Please help
and when i removed (vCreated[vehicleid] == true)
it showed the message while entering any car :(
thanks

Konstantinos
06/05/2014, 11:21 AM
OnPlayerStateChange is called with newstate as driver so the vehicle exist.

public OnPlayerStateChange(playerid, newstate, oldstate)
{
if (newstate == PLAYER_STATE_DRIVER)
{
new
vehicleid = GetPlayerVehicleID(playerid);

if (!isnull(VehicleInfo[vehicleid][vOwner]) && !strcmp(VehicleInfo[vehicleid][vOwner], PlayerName(playerid)))
{
SendClientMessage(playerid,COLOR_GREEN,"Welcome to your purchased vehicle");
}
}
return 1;
}



#if !defined isnull
#define isnull(%1) \
((!(%1[0])) || (((%1[0]) == '\1') && (!(%1[1]))))
#endif

superrobot48
06/05/2014, 11:26 AM
Not Working :( it is not showing anything

Konstantinos
06/05/2014, 11:30 AM
If it doesn't show the message, then either VehicleInfo[vehicleid][vOwner] is null or it doesn't match with the player's name.

Debug it and go test it in-game. Post what it printed to the console/server log.

printf("vOwner: \"%s\" & PlayerName: \"%s\"", VehicleInfo[vehicleid][vOwner], PlayerName(playerid));
if (!isnull(VehicleInfo[vehicleid][vOwner]) && !strcmp(VehicleInfo[vehicleid][vOwner], PlayerName(playerid)))
{
SendClientMessage(playerid,COLOR_GREEN,"Welcome to your purchased vehicle");
}

superrobot48
06/05/2014, 11:38 AM
debugged. vowner is null
[17:22:09] vOwner: "" & PlayerName: "superrobot48"

Konstantinos
06/05/2014, 11:41 AM
How do you assign the owner's name to vOwner for each vehicleid?

superrobot48
06/05/2014, 11:44 AM
format(VehicleInfo[vehicleid][vOwner], MAX_PLAYER_NAME, vehicleOwner);
And the other code works
it checks how many cars i have :)

Konstantinos
06/05/2014, 11:48 AM
Sorry, I meant where do you assign the owner's name. No owner has been set for that vehicleid.

superrobot48
06/05/2014, 11:50 AM
stock VehicleCreate(vehicleModel, Float:x,Float:y,Float:z,Float:a, vehicleColor1, vehicleColor2,vehicleOwner[])
// This is our most important function. This will create the vehicle with the valid information provided on the arguments.
{

new vehicleid = VehicleGetFreeSlot(); // Get the free slot and store it on the vehicleid variable.
//Now, we will basically put the information from the arguments to our newly created vehicle's array.
VehicleInfo[vehicleid][vModel] = vehicleModel; // Assign our vehicle's model id to the model id provided.
VehicleInfo[vehicleid][xspawn] = x;
VehicleInfo[vehicleid][yspawn] = y;
VehicleInfo[vehicleid][zspawn] = z;
VehicleInfo[vehicleid][anglespawn] = a; // Assign our vehicle's position/rotation to the position/rotation provided.
VehicleInfo[vehicleid][vColor1] = vehicleColor1; // Assign our vehicle's primary color to the primary color provided.
VehicleInfo[vehicleid][vColor2] = vehicleColor2; // Assign our vehicle's secondary color to the secondary color provided. // Assign our vehicle's respawn time to the respawn time provided.
format(VehicleInfo[vehicleid][vOwner], MAX_PLAYER_NAME, vehicleOwner); // Assign our vehicle's owner name to the owner name provided. // Assign our vehicle's lock data to the lock data provided.
VehicleInfo[vehicleid][vID] = CreateVehicle(vehicleModel, x, y, z, a, vehicleColor1, vehicleColor2, -1); // Now we are doing two things. We are creating our vehicle on the sa-mp world based on the data provided as well as assigning the
// sa-mp vehicle id to our system's vehicle id so that we can alter it later.
vCreated[vehicleid] = true; // Tell our vehicle management variable that this vehicle creation has been done and it's now on the system.
VehicleSave(vehicleid);
return vehicleid; // Return the newly created vehicle's system id.
}

Konstantinos
06/05/2014, 11:58 AM
The "vehicleid" in OnPlayerStateChange is the vehicle's ID so it might not be the same as the one you returned from VehicleGetFreeSlot.


stock VehicleCreate(vehicleModel, Float:x,Float:y,Float:z,Float:a, vehicleColor1, vehicleColor2,vehicleOwner[])
// This is our most important function. This will create the vehicle with the valid information provided on the arguments.
{

new vehicleid = VehicleInfo[vehicleid][vID] = CreateVehicle(vehicleModel, x, y, z, a, vehicleColor1, vehicleColor2, -1); // Now we are doing two things. We are creating our vehicle on the sa-mp world based on the data provided as well as assigning the
// sa-mp vehicle id to our system's vehicle id so that we can alter it later.

//Now, we will basically put the information from the arguments to our newly created vehicle's array.
VehicleInfo[vehicleid][vModel] = vehicleModel; // Assign our vehicle's model id to the model id provided.
VehicleInfo[vehicleid][xspawn] = x;
VehicleInfo[vehicleid][yspawn] = y;
VehicleInfo[vehicleid][zspawn] = z;
VehicleInfo[vehicleid][anglespawn] = a; // Assign our vehicle's position/rotation to the position/rotation provided.
VehicleInfo[vehicleid][vColor1] = vehicleColor1; // Assign our vehicle's primary color to the primary color provided.
VehicleInfo[vehicleid][vColor2] = vehicleColor2; // Assign our vehicle's secondary color to the secondary color provided. // Assign our vehicle's respawn time to the respawn time provided.
strcat((VehicleInfo[vehicleid][vOwner][0] = EOS, VehicleInfo[vehicleid][vOwner]), vehicleOwner, MAX_PLAYER_NAME); // Assign our vehicle's owner name to the owner name provided. // Assign our vehicle's lock data to the lock data provided.
VehicleSave(vehicleid);
return vehicleid; // Return the newly created vehicle's system id.
}

superrobot48
06/05/2014, 12:07 PM
i think that would mess up all the things :(

Konstantinos
06/05/2014, 12:14 PM
That's how it should be. And the code of your first post (modified) should work too:

GetPlayerVehicles(playerid)
{
new count;
for (new i = 1; i < MAX_VEHICLES; i++) // For every vehicles till the maximum amount
{
if (!GetVehicleModel(i)) continue; // doesn't exist, so continue to the next row
if (!isnull(VehicleInfo[i][vOwner]) && !strcmp(VehicleInfo[i][vOwner], PlayerName(playerid)))
{
count++;
}
}
return count;
}

superrobot48
06/05/2014, 01:05 PM
Thankyou very much , but i fixed it with :)

GetVehicleID(vehicleid)
{
for(new i=1; i < MAX_VEHICLES; i++)
{
if(vCreated[i] && VehicleInfo[i][vID] != vehicleid) return i;
}
return 0;
}