05.10.2010, 21:49
Here:
As gamer_z said, the loops would take awhile. Even if there was a 200 max player limit, and 1 player was connected with the ID of 79, random might not return 79 for quite some time. With the above code there's not a need to worry about that problem.
pawn Код:
stock RandomPlayerID()
{
new
pIDs[MAX_PLAYERS],
it[2];
for(it[0] = 0, it[1] = 0; it[0] < MAX_PLAYERS; ++it[0])
if(IsPlayerConnected(it[0]) && !IsPlayerNPC(it[0]))
{
pIDs[it[1]] = it[0];
it[1]++;
}
return (it[1] != 0) ? (pIDs[random(it[1])]) : (INVALID_PLAYER_ID);
}