05.01.2013, 15:35
I'm trying to make this FindPlayer function that finds the player ID using a string and returns INVALID_PLAYER_ID if there is no such player online. It should also return -2 or as I called it 'MULTIPLE_MATCHES' if there are more than 2 players with the same part of nick the player entered.
I have defined MULTIPLE_MATCHES as -2 BTW.
This is the code I'm using in some command for testing this FindPlayer function:
Whenever I enter "/goto yoyo" in my local server (when I'm all alone) it returns INVALID_PLAYER_ID, or in other words it says that the player is not connected.
Please tell me how to fix that or give me an alternate method of detecting the player ID through part of the name.
P.S. the IsNumeric part works fine and I can /goto players by typing their IDs.
I have defined MULTIPLE_MATCHES as -2 BTW.
pawn Код:
stock FindPlayer(string[])
{
new foundplayer = INVALID_PLAYER_ID, name[MAX_PLAYER_NAME];
if (IsNumeric(string)) {
foundplayer = strval(string);
if (IsPlayerConnected(foundplayer))
return foundplayer;
else
foundplayer = INVALID_PLAYER_ID;
}
else
{
for(new i = 0; i < MAX_PLAYERS; i++) {
GetPlayerName(i, name, sizeof(name));
if (strfind(name, string, true) != -1) {
if (foundplayer != INVALID_PLAYER_ID)
return MULTIPLE_MATCHES;
else if(foundplayer == INVALID_PLAYER_ID)
foundplayer = i;
}
}
}
return foundplayer;
}
pawn Код:
new name[128], pID;
if(sscanf(params, "s[128]", name)) return SendClientMessage(playerid, COLOR_RED, "USAGE: /goto (nick/id) - Enter a valid Nick / ID");
pID = FindPlayer(name);
if(!IsPlayerConnected(pID)) return SendClientMessage(playerid, COLOR_RED, "This player is not connected.");
new err[128];
format(err, sizeof err, "'%s' is found in multiple nicks. Please be more specific.", name);
if(pID == MULTIPLE_MATCHES) return SendClientMessage(playerid, COLOR_RED, err);
Please tell me how to fix that or give me an alternate method of detecting the player ID through part of the name.
P.S. the IsNumeric part works fine and I can /goto players by typing their IDs.