GetPlayerID(byname) bug
#1

Hi guys

I use GetPlayerID function to check is player online

For example:
Код:
new id= GetPlayerID("Gospodin");
if(IsPlayerConnected(id))
{
printf("Gospodin is online");
}
Function GetPlayerID

Код:
GetPlayerID(string[])
{
	for(new i = 0; i <= MAX_PLAYERS; i++)
	{
		if(IsPlayerConnected(i) == 1)
		{
			if(strcmp(Player_Name(i), string, true, strlen(string)) == 0)
			{
				return i;
			}
		}
	}
	return INVALID_PLAYER_ID;
}
Player_Name function(uses in GetPlayerID function)

Код:
Player_Name(id)
{
	new ime[MAX_PLAYER_NAME+1];
 	GetPlayerName(id, ime, sizeof ime);
	return ime;
}
What is bug?
I'm make check to see is "Gospodin" online,but they will give positive result if "Gospodin_" "Gospodin_X" etc are online...But I need to check just if "Gospodin" is online,no other combination...
Reply
#2

There should be a ! before strcmp.
strcmp returns value 0 if the strings match.

And the loop should not be <= MAX_PLAYERS. The playerid starts from 0 and ends at MAX_PLAYERS - 1.
That's how the indexing is done.
Reply
#3

Quote:
Originally Posted by iSpark
Посмотреть сообщение
There should be a ! before strcmp.
strcmp returns value 0 if the strings match.
That part of the code is right though.. he is still checking if it returns 0.

Try this:
pawn Код:
GetPlayerID(name[])
{
    new playerName[24];

    for(new i = 0; i < MAX_PLAYERS; i++)
    {
        if(!IsPlayerConnected(i)) continue;

        GetPlayerName(i, playerName, 24);

        if(!strcmp(playerName, playerName, false)) return i;
    }

    return INVALID_PLAYER_ID;
}
Reply
#4

Quote:
Originally Posted by iSpark
Посмотреть сообщение
There should be a ! before strcmp.
strcmp returns value 0 if the strings match.

And the loop should not be <= MAX_PLAYERS. The playerid starts from 0 and ends at MAX_PLAYERS - 1.
That's how the indexing is done.
Adding a ! or checking if its return value is 0, are both same thing.

Also, strcmp function will check if your name contains that name or not in whole, so that works that way.

Also, If you are checking IsPlayerConnected inside the GetID function, you dont have to use it at the place where you check if he is online or not. Because, It will only return the ID if player is online.

What you can do is, Use "u" format specifier for sscanf for exact name.

Код:
 sscanf(params, "u",player)
"u" specifier will check using player ID or with name.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)