• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Pawn] Function Get ID by name
#1
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?
  Reply
#2
Use sscanf - `u` already does this.
  Reply
#3
Use sscanf as mentioned in the previous post.

PHP Code:
IdByName(const name[])

{

? ? new 
playerid;

? ? 
sscanf(name"u"playerid);

? ? return 
playerid;


  Reply
#4
(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..
  Reply
#5
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`.
  Reply
#6
(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..
  Reply
#7
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;
}
Using Pawn.CMD?

If you're doing so, this is the very first sign that you absolutely shouldn't utilize your all powerful P-Code knowledge in any of the scripting discussion topics.
  Reply


Forum Jump: