Comparing two strings. -
nerovani - 24.03.2016
Hi.
I made a command which checks what vehicles a player owns. It checks that by comparing the player's name with the owner name of a vehicle which is stored in the vehicle's folder.
Код:
CMD:myvehicles(playerid)
{
new string[160],vehicles;
for(new i = 0; i < MAX_CUSTOM_VEHICLES; i++)
{
if(DVehicles[i][dModel] != 0){
if(strcmp(PlayerInfo[playerid][pRealName], DVehicles[i][vOwner], true) == 0)
{
vehicles = vehicles + 1;
}}
}
format(string,sizeof(string),"{989898}----------Vehicles (%d/3)----------",vehicles);
SendClientMessage(playerid,-1,string);
for(new i = 0; i < MAX_CUSTOM_VEHICLES; i++)
{
if(strcmp(PlayerInfo[playerid][pRealName], DVehicles[i][vOwner], true) == 0)
{
format(string,sizeof(string),"{989898}ID %d | %s",i,GetVehicleName(i));
SendClientMessage(playerid,-1,string);
}
}
return 1;
}
In one case, when ^ that ^ is executed, it gives me this:
----------Vehicles (3/3)----------
SERVER: Unknown Command
(There are 3 vehicles spawned in total in the server)
In another case, when I replace the string comparing with:
Код:
if(strcmp(PlayerInfo[playerid][pRealName], DVehicles[i][vOwner], true) == 1)
It returns me this:
----------Vehicles (0/3)----------
Any suggestions that can help?
Re: Comparing two strings. -
NaS - 24.03.2016
Add a check for comparing both strings' lengths, if one of the strings is null (empty) strcmp will return 0 too.
Or just check if the vehicle's owner name is longer than 2 chars before comparing with the player's name.
PS. your string array is too big (chat messages can't be longer than 128 chars anyways).
Re: Comparing two strings. -
nerovani - 24.03.2016
Both strings have the lenght of "MAX_PLAYER_NAME".
Re: Comparing two strings. -
NaS - 24.03.2016
Quote:
Originally Posted by nerovani
Both strings have the lenght of "MAX_PLAYER_NAME".
|
Their maximum length is 24 (MAX_PLAYER_NAME), but not their actual lengths, since not all player names have 24 characters.
If noone owns the car and you didnt put anything into the array, it will be empty (NULL) and lead to strcmp returning 0.
Re: Comparing two strings. -
Konstantinos - 24.03.2016
Except the isnull check NaS mentioned, you know that the max vehicles a player can have is 3 so limit the loop.
The unknown command is due to a run time error. It's either MAX_CUSTOM_VEHICLES is defined with a value greater than the size of DVehicles or GetVehicleName function does not check if the vehicle modelid is valid (400-611) and goes with a negative index at -400. Use crashdetect plugin so you can be sure.
PHP код:
CMD:myvehicles(playerid)
{
new string[43], vehicles[3] = {-1, ...}, count = -1;
for (new i = 0; i < sizeof DVehicles; i++)
{
if (DVehicles[i][dModel] && !isnull(DVehicles[i][vOwner]) && !strcmp(PlayerInfo[playerid][pRealName], DVehicles[i][vOwner], true))
{
vehicles[++count] = i;
}
}
format(string, sizeof(string), "{989898}----------Vehicles (%d/3)----------", count + 1);
SendClientMessage(playerid, -1, string);
for (new i = 0, j = count; i <= j; i++)
{
format(string, sizeof(string), "{989898}ID %d | %s", vehicles[i], GetVehicleName(vehicles[i]));
SendClientMessage(playerid, -1, string);
}
return 1;
}
Re: Comparing two strings. -
Dokins - 24.03.2016
OFF TOPIC:
A small suggestion might be that it's probably much better to use an ID (integer) to compare ownership, it's also much easier and although this isn't extremely important, it does use less data.
Re: Comparing two strings. -
nerovani - 24.03.2016
It works perfect now, thank you guys for your help.