View Full Version : Selecting Players

07/05/2012, 05:57 PM

Joe Staff
07/05/2012, 06:19 PM
Try something like this:

stock RandomPlayer(players[],size=MAX_PLAYERS)
new random=random(strlen(players))
new result=players[random]-1; //We subtract one here to equilize for EOS being 0, explained later
return result;

This will sequentially choose a random player, and then remove him from the array.
Here's how you'd use it.

//Create your array
new playerarray[MAX_PLAYERS+1]; //+1 because we're treating it as a string, and strings have an 'EOS'

//Create an index and add the players to the array
new idx;
for(new playerid;playerid<MAX_PLAYERS;playerid++)
if(IsPlayerConnected(playerid)&&!IsPlayerNPC(playerid)) //Only want actual players who are online
playerarray[idx]=playerid+1; //Plus 1 because EOS is 0, and player 0 is an ID
//Now your array is full of online players

//Now use the function
printf("Player Selected: %d",RandomPlayer(playerarray));
printf("Player Selected: %d",RandomPlayer(playerarray));
printf("Player Selected: %d",RandomPlayer(playerarray));

Will print

Player Selected: 0
Player Selected: 6
Player Selected: 13

It will never select the same player twice.

Not tested