SA-MP Forums Archive
[Include] PlayerLoop - Skips unconnected players - 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: Filterscripts (https://sampforum.blast.hk/forumdisplay.php?fid=17)
+---- Forum: Includes (https://sampforum.blast.hk/forumdisplay.php?fid=83)
+---- Thread: [Include] PlayerLoop - Skips unconnected players (/showthread.php?tid=520569)



PlayerLoop - Skip unconnected players - RedFusion - 19.06.2014

Loop through the connected players only, instead of looping through every playerid with this tiny script.
pawn Код:
/******************************************************************************/

new pl_Players[MAX_PLAYERS] = {INVALID_PLAYER_ID, ...},
    pl_Connected,
    pl_MaxPlayers;

/******************************************************************************/

#define GetIndexPlayerID(%0) pl_Players[%0]
#define GetConnectedPlayers pl_Connected

/******************************************************************************/

public OnFilterScriptInit()
{
    pl_MaxPlayers = GetMaxPlayers();
    for(new playerid; playerid < pl_MaxPlayers; playerid ++)
    {
        if(IsPlayerConnected(playerid))
        {
            pl_Players[pl_Connected] = playerid;
            pl_Connected ++;
        }
    }
    #if defined pl_OnFilterScriptInit
        return pl_OnFilterScriptInit();
    #else
        return 1;
    #endif
}
#if defined pl_OnFilterScriptInit
    forward pl_OnFilterScriptInit();
#endif
#if defined _ALS_OnFilterScriptInit
    #undef OnFilterScriptInit
#else
    #define _ALS_OnFilterScriptInit
#endif
#define OnFilterScriptInit pl_OnFilterScriptInit

/******************************************************************************/

public OnGameModeInit()
{
    pl_MaxPlayers = GetMaxPlayers();
    #if defined pl_OnGameModeInit
        return pl_OnGameModeInit();
    #else
        return 1;
    #endif
}
#if defined pl_OnGameModeInit
    forward pl_OnGameModeInit();
#endif
#if defined _ALS_OnGameModeInit
    #undef OnGameModeInit
#else
    #define _ALS_OnGameModeInit
#endif
#define OnGameModeInit pl_OnGameModeInit

/******************************************************************************/

public OnPlayerConnect(playerid)
{
    pl_Players[pl_Connected] = playerid;
    pl_Connected ++;
    #if defined pl_OnPlayerConnect
        return pl_OnPlayerConnect(playerid);
    #else
        return 1;
    #endif
}
#if defined pl_OnPlayerConnect
    forward pl_OnPlayerConnect(playerid);
#endif
#if defined _ALS_OnPlayerConnect
    #undef OnPlayerConnect
#else
    #define _ALS_OnPlayerConnect
#endif
#define OnPlayerConnect pl_OnPlayerConnect

/******************************************************************************/

public OnPlayerDisconnect(playerid, reason)
{
    pl_Connected = 0;
    for(new id; id < pl_MaxPlayers; id ++)
    {
        if(IsPlayerConnected(id) && playerid != id)
        {
            pl_Players[pl_Connected] = id;
            pl_Connected ++;
        }
    }
    for(new index = pl_Connected; index < pl_MaxPlayers; index ++)
        pl_Players[index] = INVALID_PLAYER_ID;
    #if defined pl_OnPlayerDisconnect
        return pl_OnPlayerDisconnect(playerid, reason);
    #else
        return 1;
    #endif
}
#if defined pl_OnPlayerDisconnect
    forward pl_OnPlayerDisconnect(playerid, reason);
#endif
#if defined _ALS_OnPlayerDisconnect
    #undef OnPlayerDisconnect
#else
    #define _ALS_OnPlayerDisconnect
#endif
#define OnPlayerDisconnect pl_OnPlayerDisconnect

/******************************************************************************/
Example usage:
pawn Код:
for(new i = 0; i < GetConnectedPlayers; i ++)
{
    new playerid = GetIndexPlayerID(i);
    printf("playerid %i is connected", playerid);
}



Re: PlayerLoop - Skips unconnected players - Richie© - 19.06.2014

Ok, i guess. But ill stick to foreach.


Re: PlayerLoop - Skips unconnected players - SimonItaly - 19.06.2014

"Reinventing the wheel" again.


Re: PlayerLoop - Skips unconnected players - RedFusion - 19.06.2014

I consider it to be a lightweight alternative to foreach.


Respuesta: PlayerLoop - Skips unconnected players - Swedky - 19.06.2014

Speed test? With a normal loop and y_iterate (and your include, sure)...


Re: PlayerLoop - Skips unconnected players - Kar - 19.06.2014

One issue, indexes in pl_Players is never reset.


Re: PlayerLoop - Skips unconnected players - RedFusion - 19.06.2014

It doesn't need to be reset


Re: PlayerLoop - Skips unconnected players - [WSF]ThA_Devil - 19.06.2014

What happens if, let's say server has 10 players,
5 quit.
Online:
0,1,5,6,9


Re: PlayerLoop - Skips unconnected players - Kar - 19.06.2014

Quote:
Originally Posted by [WSF]ThA_Devil
Посмотреть сообщение
What happens if, let's say server has 10 players,
5 quit.
Online:
0,1,5,6,9
then indexes 0,1,2,3,4 will contain 0,1,5,6,9 respectively. It will work, and I'm not even going to argue.

From what I've seen RedFusion only aims for the point of working, he doesn't really aim for things like speed and safety.


Re: PlayerLoop - Skips unconnected players - RedFusion - 19.06.2014

As long as you stop the loop at GetConnectedPlayers as shown in the example you're fine. But just for the sake of safety the array is completely reset at disconnect


Re: PlayerLoop - Skips unconnected players - Kar - 19.06.2014

pawn Код:
for(new id = 0; id < pl_MaxPlayers; id ++)
    {
        if(IsPlayerConnected(id) && playerid != id)
            pl_Players[pl_Connected++] = id;
        else
            pl_Players[id] = INVALID_PLAYER_ID;
    }



Re: PlayerLoop - Skips unconnected players - RedFusion - 19.06.2014

Quote:
Originally Posted by Kar
Посмотреть сообщение
pawn Код:
for(new id = 0; id < pl_MaxPlayers; id ++)
    {
        if(IsPlayerConnected(id) && playerid != id)
        {
            pl_Players[pl_Connected] = id;
            pl_Connected ++;
        }
        else
            pl_Players[id] = INVALID_PLAYER_ID;
    }
This MIGHT not work. Imagine this scenario, because this is what your code would do:
Код:
Playerid 0 Connected, pl_Players[0] = 0
Playerid 1 Not connected, pl_Players[1] = INVALID_PLAYER_ID
Playerid 2 Connected, pl_Players[1] = 2
Playerid 3 Connected, pl_Players[2] = 3
Playerid 4 Not connected, pl_Players[4] = INVALID_PLAYER_ID
... And the rest of the players are disconnected.
So what happened to Index 3? Nothing.


Re: PlayerLoop - Skips unconnected players - Kar - 19.06.2014

Hmm, okay

what you did was better too

for(new index = pl_Connected; index < pl_MaxPlayers; index ++)
pl_Players[index] = INVALID_PLAYER_ID;

gj


Re: PlayerLoop - Skips unconnected players - iRaiDeN - 20.06.2014

So do not make much use of it, in fact I'm not saying it's not useful, now exist in various forms loops like: foreach which is the fastest that exists today.


Re: PlayerLoop - Skips unconnected players - StreetGT - 20.06.2014

foreach? ...


Re: PlayerLoop - Skips unconnected players - iRaiDeN - 20.06.2014

Quote:
Originally Posted by StreetGT
Посмотреть сообщение
foreach? ...
& y_iterate.


Re: PlayerLoop - Skips unconnected players - Patrick - 20.06.2014

Quote:
Originally Posted by iRaiDeN
Посмотреть сообщение
& y_iterate.
why and? y_iterate and foreach are the same. y_iterate is just a name to go with YSI.


Re: PlayerLoop - Skips unconnected players - iRaiDeN - 20.06.2014

Quote:
Originally Posted by Patrick_
Посмотреть сообщение
why and? y_iterate and foreach are the same. y_iterate is just a name to go with YSI.
I do not use any - that these supplements, I just know them.


Re: PlayerLoop - Skips unconnected players - RedFusion - 21.06.2014

How is y_iterate faster in adding players?


Re : PlayerLoop - Skips unconnected players - S4t3K - 21.06.2014

y_iterate uses a pawn feature called states I think.