Find player by name
#1

How do I find player by his name?
I know that you can use sscand but i can only make it work with command, i want to search player by name in a function
Reply
#2

pawn Code:
stock GetName(playerid)
{
    new name[MAX_PLAYER_NAME];
    GetPlayerName(playerid, name, MAX_PLAYER_NAME);
    return name;
}

stock GetPlayerIdFromName(const playername[]) {
    foreach(new i : Player) {
        if(!strcmp(GetName(i), playername, true, strlen(playername)))
        {
            return i;
        }
    }
    return INVALID_PLAYER_ID;
}
Reply
#3

Make sure if you use algorhitmically's code, which will work perfectly... make sure to remove the word "stock" from the start of the functions.
Reply
#4

To give some extra info about stock: Using keyword stock you indeed tell the compiler the piece of code is not required to use (thus not showing a warning if it's unused).
In gamemodes, you usually only create functions when you need them so the use of stock is really not needed. 'stock' is not a keyword you need to create a function. You can even declare variables as stock.

Apart from that:
pawn Code:
GetIDFromName(const name[])
{
    new ret_id;
    sscanf(name, "u", ret_id);
    return ret_id;
}
INVALID_PLAYER_ID is returned if the player wasn't found. -1 is returned if no name was entered. Otherwise it returns the playerid.
The upside of this is that it is faster than looping through players, comparing their name in strcmp(). And partly names work (if there aren't multiple matches).
If player "SuperPlayerxTF" is online, using GetIDFromName("Super") would return the ID of SuperPlayerxTF.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)