SA-MP Forums Archive
need help with ReturnUser function - 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: need help with ReturnUser function (/showthread.php?tid=662260)



need help with ReturnUser function - Stefhan - 27.12.2018

So I have this function ReturnUser, which is ment to convert a username ( if online ) to an ID so I can use IsPlayerConnected with strings instead of IDs. ( Useful when I for example want to make a set admin level command for offline players. )

The issue is, that it finishes off my strings to find players online. So if I want to do /omakeadmin stef 1, it will finish the string to Stefhan ( if he is online, otherwise it wont finish the line ) instead of looking for Stef.

How do I change this function so it doesn't finish off names?

Код:
ReturnUser(text[])
{
    new smalltext[MAX_PLAYER_NAME];
    strmid(smalltext, text, 0, MAX_PLAYER_NAME); //extract from 0 to 24, put in smalltext
    //check if id 0
    if(!strcmp(smalltext, "0"))
    {
        if(IsPlayerConnected(0))
        {
            return 0; //return id 0
        }
        else
        {
            return INVALID_PLAYER_ID;
        }
    }
    //convert str to number
    new id = strval(smalltext);
    if(id < 0)
    {
        return INVALID_PLAYER_ID;
    }
    if(id)   //is an id
    {
        if(IsPlayerConnected(id))
        {
            return id;
        }
        else
        {
            return INVALID_PLAYER_ID;
        }
    }
    else     //if part of a players name
    {
        new playerName[MAX_PLAYER_NAME];
        foreach(Player, i)
        {
            GetPlayerName(i, playerName, MAX_PLAYER_NAME);
            if(!strcmp(playerName, smalltext, true, strlen(smalltext)))
            {
                return i;
            }
        }
    }
    return INVALID_PLAYER_ID;
}



Re: need help with ReturnUser function - Dayrion - 27.12.2018

Have you heard about sscanf?