SA-MP Forums Archive
Find player by name - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Find player by name (/showthread.php?tid=677040)



Find player by name - PowerF - 28.06.2020

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


Re: Find player by name - algorhitmically - 28.06.2020

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;
}



Re: Find player by name - SkyFlare - 28.06.2020

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.


Re: Find player by name - Kwarde - 28.06.2020

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.