Burgershot
  • Home
  • Members
  • Team
  • Help
  • Search
  • Register
  • Login
  • Home
  • Members
  • Help
  • Search
Burgershot SA-MP Pawn Scripting [Pawn] Function Get ID by name

 
  • 0 Vote(s) - 0 Average
Pawn Function Get ID by name
GospodinX
Offline

Burgershot Member
Posts: 10
Threads: 5
Joined: Dec 2020
Reputation: 0
Location: Bosna i Hercegovina
#1
2021-03-16, 01:34 PM
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?
Y_Less
Offline

Administrator

Posts: 323
Threads: 16
Joined: Feb 2019
Reputation: 90
#2
2021-03-16, 01:42 PM
Use sscanf - `u` already does this.
Radical
Offline

Burgershot Member
Posts: 148
Threads: 21
Joined: Dec 2020
Reputation: 16
#3
2021-03-16, 03:39 PM
Use sscanf as mentioned in the previous post.
PHP Code:
IdByName(const name[])
{
    new playerid;
    sscanf(name, "u", playerid);
    return playerid;
} 
GospodinX
Offline

Burgershot Member
Posts: 10
Threads: 5
Joined: Dec 2020
Reputation: 0
Location: Bosna i Hercegovina
#4
2021-03-16, 04:22 PM
(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..
Y_Less
Offline

Administrator

Posts: 323
Threads: 16
Joined: Feb 2019
Reputation: 90
#5
2021-03-16, 04:30 PM
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`.
GospodinX
Offline

Burgershot Member
Posts: 10
Threads: 5
Joined: Dec 2020
Reputation: 0
Location: Bosna i Hercegovina
#6
2021-03-16, 06:39 PM
(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..
Pinch
Offline

Burgershot Member
Posts: 391
Threads: 19
Joined: Apr 2019
Reputation: 22
Location: Belgrade, Serbia
#7
2021-03-17, 03:01 AM (This post was last modified: 2021-03-17, 03:13 AM by Pinch.)
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.
« Next Oldest | Next Newest »



  • View a Printable Version
  • Subscribe to this thread
Forum Jump:

© Burgershot - Powered by our Community and MyBB Original Theme by Emerald

Linear Mode
Threaded Mode