SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help

Reply
 
Thread Tools Display Modes
Old 24/03/2016, 10:35 AM   #1
nerovani
Big Clucker
 
nerovani's Avatar
 
Join Date: May 2013
Location: Croatia
Posts: 93
Reputation: 1
Default Comparing two strings.

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.

Code:
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:
Code:
if(strcmp(PlayerInfo[playerid][pRealName], DVehicles[i][vOwner], true) == 1)
It returns me this:

----------Vehicles (0/3)----------

Any suggestions that can help?
nerovani is offline   Reply With Quote
Old 24/03/2016, 10:47 AM   #2
NaS
High-roller
 
NaS's Avatar
 
Join Date: Mar 2008
Location: 🇩🇪
Posts: 1,652
Reputation: 559
Default Re: Comparing two strings.

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).
NaS is offline   Reply With Quote
Old 24/03/2016, 10:49 AM   #3
nerovani
Big Clucker
 
nerovani's Avatar
 
Join Date: May 2013
Location: Croatia
Posts: 93
Reputation: 1
Default Re: Comparing two strings.

Both strings have the lenght of "MAX_PLAYER_NAME".
nerovani is offline   Reply With Quote
Old 24/03/2016, 11:11 AM   #4
NaS
High-roller
 
NaS's Avatar
 
Join Date: Mar 2008
Location: 🇩🇪
Posts: 1,652
Reputation: 559
Default Re: Comparing two strings.

Quote:
Originally Posted by nerovani View Post
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.
NaS is offline   Reply With Quote
Old 24/03/2016, 11:13 AM   #5
Konstantinos
Spam Machine
 
Konstantinos's Avatar
 
Join Date: Dec 2011
Posts: 11,844
Reputation: 1399
Default Re: Comparing two strings.

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 Code:
CMD:myvehicles(playerid)
{
    new 
string[43], vehicles[3] = {-1, ...}, count = -1;

    for (new 
0sizeof DVehiclesi++)
    {
        if (
DVehicles[i][dModel] && !isnull(DVehicles[i][vOwner]) && !strcmp(PlayerInfo[playerid][pRealName], DVehicles[i][vOwner], true))
        {
            
vehicles[++count] = i;
        }
    }
  
    
format(stringsizeof(string), "{989898}----------Vehicles (%d/3)----------"count 1);
    
SendClientMessage(playerid, -1string);
  
    for (new 
0count<= ji++)
    {
        
format(stringsizeof(string), "{989898}ID %d | %s"vehicles[i], GetVehicleName(vehicles[i]));
        
SendClientMessage(playerid, -1string);
    }
    return 
1;

__________________
Life is like riding a bicycle. To keep your balance, you must keep moving.

[Tutorial] How to use SQLite
[FilterScript] Tune System
Konstantinos is offline   Reply With Quote
Old 24/03/2016, 11:43 AM   #6
Dokins
Banned
 
Join Date: Jun 2010
Posts: 1,648
Reputation: 53
Default Re: Comparing two strings.

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.
Dokins is offline   Reply With Quote
Old 24/03/2016, 12:05 PM   #7
nerovani
Big Clucker
 
nerovani's Avatar
 
Join Date: May 2013
Location: Croatia
Posts: 93
Reputation: 1
Default Re: Comparing two strings.

It works perfect now, thank you guys for your help.
nerovani is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Comparing 2 strings using strcmp. Black Axe Scripting Help 2 20/01/2016 08:26 AM
Comparing 2 strings Aa12 Scripting Help 8 08/01/2016 06:20 PM
comparing strings HondaCBR Scripting Help 2 02/01/2012 03:15 PM
Help with comparing two strings OUL Scripting Help 2 02/11/2011 06:11 PM
[Help] Comparing strings (strcmp) Maxips2 Help Archive 2 03/08/2010 10:46 AM


All times are GMT. The time now is 09:55 PM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.