There's a reason the theory in the first post wont work. You will need a variable that will store the highest player ID in such case, because IDs are not rearranged in the server if someone with a lower ID decides to reconnect. For increased efficiency, as others already pointed out, why not use the foreach solution by ******? It can be fairly useful for other things in your script as well, since it handles linked lists.
The slowest way to do your loops would be:
pawn Код:
for(new i = 0; i != GetMaxPlayers(); i++)
Since this will call GetMaxPlayers() exactly the amount of players allowed on your server. And as we know, calling even native funcs like this, in comparison to other methods, is time costly.
If you don't have the chance to determine a valid MAX_PLAYERS in the code, then creating an extra variable will help. You can do this prior to the loop or even load the value in OnGameModeInit/OnFilterScriptInit or whatever is called when your library loads.
pawn Код:
for(new i = 0, j = GetMaxPlayers(); i != j; i++)
And then the best solution for people that know the amount of their slots.
pawn Код:
for(new i = 0; i != MAX_PLAYERS; i++)
Just remember to redefine your MAX_PLAYERS as kadaradam showed above:
pawn Код:
#undef MAX_PLAYERS
#define MAX_PLAYERS 100
Also, keep in mind that when you want to make your loops faster, there are numerous things that people often oversee. Imagine a system where there's something like:
pawn Код:
#define NO_MINIGAME 1
#define MINIGAME_FALLOUT 2
public OnPlayerConnect(playerid)
{
pState[playerid] = NO_MINIGAME;
}
public OnPlayerDisconnect(playerid)
{
pState[playerid] = 0;
}
You'll notice that for a connected player, pState is never 0. You can make your loops faster when they look like this:
pawn Код:
for(new i = 0; i != MAX_PLAYERS; i++)
{
if(IsPlayerConnected(i) && pState[i] == MINIGAME_FALLOUT)
{
}
}
Now an easy thing to consider: can pState be MINIGAME_FALLOUT for a not connected player slot? Nope. This makes calling IsPlayerConnected unnecessary, since pState[playerid] == MINIGAME_FALLOUT check already involves checking if: 1. player is connected, 2. player is in the minigame!
This can be simplified to:
pawn Код:
for(new i = 0; i != MAX_PLAYERS; i++)
{
if(pState[i] == MINIGAME_FALLOUT)
{
}
}
It happens regularly to me that when I go through some of my code that's perhaps even a year old in my gamemode, I find things like this that could use theoretical improvement. I use foreach though, so not on player loops.