open.mp forum
[Pawn] Function Get ID by name - Printable Version

+ open.mp forum (https://forum.open.mp)
-- Forum: SA-MP (https://forum.open.mp/forumdisplay.php?fid=3)
--- Forum: Pawn Scripting (https://forum.open.mp/forumdisplay.php?fid=10)
--- Thread: [Pawn] Function Get ID by name (/showthread.php?tid=1776)



Function Get ID by name - GospodinX - 2021-03-16

I have a function IDByName, as the name of the function says I want to get id by player name.



Function:

Code:
IdByName(const name[])

{

new iPlayer,names[MAX_PLAYER_NAME];

iPlayer = 999;

foreach(new i : Player)

{

? ? GetPlayerName(i, names, sizeof(names));

? ? if(!strcmp(name, names, true))

? ? {

? ? ? ? iPlayer = i;

}

}

return iPlayer;

}



Example:

Code:
? new oie = IdByName(name);

if(IsPlayerConnected(oie)) //player is online and oie is his ID

{

//code

}

else //player is not online

{

//code

}



The function was work perfectly for me for a long time. But now when I have 200 players on my server function get bugged and many times when a player is connected function can't found them..



Have anyone ideas why it happened and how I can improve my function?


RE: Function Get ID by name - Y_Less - 2021-03-16

Use sscanf - `u` already does this.


RE: Function Get ID by name - Radical - 2021-03-16

Use sscanf as mentioned in the previous post.

PHP Code:
IdByName(const name[])

{

? ? new 
playerid;

? ? 
sscanf(name"u"playerid);

? ? return 
playerid;





RE: Function Get ID by name - GospodinX - 2021-03-16

(2021-03-16, 01:42 PM)Y_Less Wrote: Use sscanf - `u` already does this.



Of course I use sscanf,but not for this.



For example I have two players in database?

GospodinX (online)

Gospodin (offline)



I want to check is Gospodin online.



If I use sscanf u "Gospodin"

He will get me id of GospodinX but i dont want it.



Am I right?

Problem is with player which have smiliar names..


RE: Function Get ID by name - Y_Less - 2021-03-16

Hmm, I see. That should be something sscanf can do - in fact I thought it was. I thought `MATCH_NAME_PARTIAL` would change that, but it seems that just changes between searching at the start of a name and in the middle of a name. I'll have to add `MATCH_NAME_EXACT`.


RE: Function Get ID by name - GospodinX - 2021-03-16

(2021-03-16, 04:30 PM)Y_Less Wrote: Hmm, I see.? That should be something sscanf can do - in fact I thought it was.? I thought `MATCH_NAME_PARTIAL` would change that, but it seems that just changes between searching at the start of a name and in the middle of a name.? I'll have to add `MATCH_NAME_EXACT`.



It would be useful.



So I dont know how to fix it now..


RE: Function Get ID by name - Pinch - 2021-03-17

Code:
IdByName(const name[])
{
    new
         iPlayer = INVALID_PLAYER_ID,
         names[MAX_PLAYER_NAME];
        
    foreach(new i : Player)
    {
        GetPlayerName(i, names, sizeof(names));
        
        if(strcmp(name, names, true)) {
            continue;
        }
            
        iPlayer = i;
        break;
    }
    return iPlayer;
}
Idk if this will help but at least it's code

...or...

Code:
IdByName(const name[]) //Lol
{
    new
        tmpID = INVALID_PLAYER_ID,
        tmpName[MAX_PLAYER_NAME];
        
    if (!sscanf(name, "r", tmpID))
    {
        GetPlayerName(tmpID, tmpName, sizeof(tmpName));
        
        if (strcmp(name, tmpName, true)) {
            tmpID = INVALID_PLAYER_ID;
        }
    }
    return tmpID;
}

...or...

Code:
IdByName(const name[]) //Lol v2
{
    new
        tmpID = INVALID_PLAYER_ID,
        tmpName[MAX_PLAYER_NAME];
        
    sscanf(name, "r", tmpID);
    
    if (!GetPlayerName(tmpID, tmpName, sizeof(tmpName)) {
        return tmpID;
    }

    if (strcmp(name, tmpName, true)) {
        tmpID = INVALID_PLAYER_ID;
    }
    
    return tmpID;
}