Selecting Players - Printable Version
+- SA-MP Forums Archive (
https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (
https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (
https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Selecting Players (
/showthread.php?tid=340531)
Selecting Players -
Kitten - 07.05.2012
Solved
Re: Selecting Players -
Joe Staff - 07.05.2012
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