[Include] PlayerLoop - Skips unconnected players
#1

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);
}
Reply
#2

Ok, i guess. But ill stick to foreach.
Reply
#3

"Reinventing the wheel" again.
Reply
#4

I consider it to be a lightweight alternative to foreach.
Reply
#5

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

One issue, indexes in pl_Players is never reset.
Reply
#7

It doesn't need to be reset
Reply
#8

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

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.
Reply
#10

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
Reply
#11

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;
    }
Reply
#12

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.
Reply
#13

Hmm, okay

what you did was better too

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

gj
Reply
#14

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.
Reply
#15

foreach? ...
Reply
#16

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

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.
Reply
#18

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.
Reply
#19

How is y_iterate faster in adding players?
Reply
#20

y_iterate uses a pawn feature called states I think.
Reply


Forum Jump:


Users browsing this thread: 5 Guest(s)