Selecting Players
#1

Solved
Reply
#2

Try something like this:

pawn Код:
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
    strdel(players,random,random+1);    
    return result;
}
This will sequentially choose a random player, and then remove him from the array.
Here's how you'd use it.
pawn Код:
//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
        idx++;
    }
}
//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
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)