SA-MP Forums Archive
Function GetSuperior/InferiorID - 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: Function GetSuperior/InferiorID (/showthread.php?tid=603791)



Function GetSuperior/InferiorID - YouServ - 27.03.2016

Hi, I have a problem with my function GetSuperior(Inferior)Id. Result of a debug :

[DEBUG SUNSHINE (0)]: SUPERIOR 0 | INFERIOR 499

Result required : SUPERIOR 1 | INFERIOR 1
(We are 2 connected, me (ID0) and a player (ID1)).

Can you help me ?

PHP код:
stock GetSuperiorId(playerid)
{
    new
        
player;
    do if(
playerid >= MAX_PLAYERSplayer 0; else player++;
    while(!
IsPlayerConnected(player) && PlayerInfo[player][LoggedIn] == false && PlayerInfo[player][pInMenu] == true && playerid == player);
    return 
playerid;
}
stock GetInferiorId(playerid)
{
    new
        
player;
    do if(
playerid <= 0player 499; else player--;
    while(!
IsPlayerConnected(player) && PlayerInfo[player][LoggedIn] == false && PlayerInfo[player][pInMenu] == true && playerid == player);
    return 
player;




Re: Function GetSuperior/InferiorID - SickAttack - 27.03.2016

pawn Код:
stock GetNextPlayerID(id)
{
    for(new i = (id + 1), j = GetPlayerPoolSize(); i <= j; i ++)
    {
        if(!IsPlayerConnected(i))
        {
            continue;
        }
       
        return i;
    }
    return INVALID_PLAYER_ID;
}



Re: Function GetSuperior/InferiorID - YouServ - 27.03.2016

For previous ID ?


Re: Function GetSuperior/InferiorID - SickAttack - 27.03.2016

Quote:
Originally Posted by YouServ
Посмотреть сообщение
For previous ID ?
Reverse the function.




Re: Function GetSuperior/InferiorID - YouServ - 27.03.2016

PHP код:
stock GetNextPlayerID(id)
{
    for(new 
= (id 1), GetPlayerPoolSize(); <= j--)
    {
        if(!
IsPlayerConnected(i))
        {
            continue;
        }
        
        return 
i;
    }
    return 
INVALID_PLAYER_ID;

That ?


Re: Function GetSuperior/InferiorID - Konstantinos - 27.03.2016

You can also use functions from foreach/y_iterate if you already use it since there are Iter_Next and Iter_Prev in it.

PHP код:
GetNextPlayerID(id)
{
    new
        
next_id;
    if ((
next_id Iter_Next(Playerid)) != MAX_PLAYERS) return next_id;
    return ((
next_id Iter_First(Player)) == id) ? INVALID_PLAYER_ID next_id;
}
GetPreviousPlayerID(id)
{
    new
        
previous_id;
    if ((
previous_id Iter_Prev(Playerid)) != MAX_PLAYERS) return previous_id;
    return ((
previous_id Iter_Last(Player)) == id) ? INVALID_PLAYER_ID previous_id;




Re: Function GetSuperior/InferiorID - SickAttack - 27.03.2016

pawn Код:
stock GetNextPlayerID(id)
{
    for(new i = (id + 1), j = GetPlayerPoolSize(); i <= j; i ++)
    {
        if(!IsPlayerConnected(i))
        {
            continue;
        }
       
        return i;
    }
    return INVALID_PLAYER_ID;
}

stock GetPreviousPlayerID(id)
{
    for(new i = (id - 1), j = 0; i >= j; i --)
    {
        if(!IsPlayerConnected(i))
        {
            continue;
        }
       
        return i;
    }
    return INVALID_PLAYER_ID;
}
"INVALID_PLAYER_ID" will be returned if there isn't a next or previous player.


Re: Function GetSuperior/InferiorID - YouServ - 27.03.2016

Nice thanks, and have you idea for make a function to get the small ID connected in the server ?


Re: Function GetSuperior/InferiorID - SickAttack - 27.03.2016

The lowest player ID?
pawn Код:
stock GetLowestPlayerID()
{
    for(new i = 0, j = GetPlayerPoolSize(); i <= j; i ++)
    {
        if(!IsPlayerConnected(i))
        {
            continue;
        }
       
        return i;
    }
    return INVALID_PLAYER_ID;
}