01.11.2015, 13:40
For example, there are 3 players on your server:
0 - You
1 - Player on derby round
2 - Someone else (we don't want to spectate him)
Iter_Random works with whole iterable and doesn't pop items from it. And that means that one item may appear two or more times in a row when we call Iter_Random
Let's run it. For example:
The second example:
You shold use recursion (warning! may caise an infinite loop if there are no players on derby) / create separate iterable for players on derby / simply use "for" loop / use another algorythm for better performance (no time to explain it)
0 - You
1 - Player on derby round
2 - Someone else (we don't want to spectate him)
Iter_Random works with whole iterable and doesn't pop items from it. And that means that one item may appear two or more times in a row when we call Iter_Random
Let's run it. For example:
PHP код:
new specplayer = Iter_Random(Player); // "specplayer" now contains 0 (chance 33.3%)
if(specplayer == playerid) // 0 is you, so this condition works
{
specplayer = Iter_Random(Player); // We get another random player and get 0 again (yes, it's possible)
}
else // this part of code doesn't execute because specplayer == playerid
{
if(OnDerby[specplayer] == false)
{
specplayer = Iter_Random(Player);
}
else
{
StartSpectate(playerid, specplayer);
}
}
// ...and we have specplayer = 0, which is you (it's an error)
PHP код:
new specplayer = Iter_Random(Player); // now we got 2 - the player we don't want to spectate
if(specplayer == playerid) // this is false because 2 != 0
{
specplayer = Iter_Random(Player);
}
else // this part of code is executing
{
if(OnDerby[specplayer] == false) // We are checking if player 2 is on derby and getting false
{
specplayer = Iter_Random(Player); // ...so we are asking for a new player... and get 2 again
}
else // this part of code doesn't run
{
StartSpectate(playerid, specplayer);
}
}
// ...and now we got "specplayer" = 2 whick is not a player on derby