Finding player ID through part of the name
#1

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.
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;
}
This is the code I'm using in some command for testing this FindPlayer function:
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);
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.
Reply
#2

PHP код:
if(sscanf(params"u"pID)) return SendClientMessage(playeridCOLOR_RED"USAGE: /goto (nick/id) - Enter a valid Nick / ID"); 
This will do your work?
Reply
#3

@coakidoo, no because it won't find the player ID if I will type part of the name that is not in the beginning.
e.g. if my name is YoYo123 and I type "/goto o" it won't find the player ID because it's not in the name beginning.
Reply
#4

That is very useless becuase large number of players on server will have "o" in their name.
Reply
#5

Quote:
Originally Posted by YoYo123
Посмотреть сообщение
@coakidoo, no because it won't find the player ID if I will type part of the name that is not in the beginning.
e.g. if my name is YoYo123 and I type "/goto o" it won't find the player ID because it's not in the name beginning.
New sscanf 2.8 does in fact support this.
Reply
#6

@Vince I am using the new sscanf 2.8.1 but it doesn't seem to work with part of the name that is not in the beginning.. I have updated the plugin, the new sscanf include and put 'plugins sscanf' in server.cfg etc. It's not working that way though
Reply
#7

Bump....
Reply


Forum Jump:


Users browsing this thread: 5 Guest(s)