22.10.2017, 10:20
(
Последний раз редактировалось RIDE2DAY; 22.10.2017 в 13:09.
)
Quote:
Код:
GetRandomPlayer(loop=3) { RandomCase: loop--; if(loop < 0)return -1; new iID = random(GetPlayerPoolSize()); if(IsPlayerConnected(iID))return iID; else goto RandomCase; } |
PHP код:
new iID = random( GetPlayerPoolSize() + 1);
EDIT - @OneDay:
Try to compile it, you'll get a nice error. I tried to give an answer in context, anyway, [HLF]Southclaw mentioned Iter_Random just a few comments above!
I know y_iterate would be the way to go (I'm using it actually) but as pawnoholic said: not many people use YSI. So, you might use something like this:
PHP код:
new bool:g_ConnectedPlayers[MAX_PLAYERS char];
public OnPlayerConnect(playerid)
{
g_ConnectedPlayers{playerid} = true;
return 1;
}
public OnPlayerDisconnect(playerid)
{
g_ConnectedPlayers{playerid} = false;
return 1;
}
GetRandomPlayer()
{
new idx;
new true_list[MAX_PLAYERS];
for(new x = 0, t = GetPlayerPoolSize(); x < t; x++)
{
if(!g_ConnectedPlayers{x}) continue;
true_list[idx] = x;
idx++;
}
if(idx)
{
return true_list[random(idx)];
}
return INVALID_PLAYER_ID;
}