Random ID's - 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: Random ID's (
/showthread.php?tid=299610)
Random ID's -
Luis- - 26.11.2011
How would I go about getting a random player using ID's?
Re: Random ID's -
SlashQ - 26.11.2011
Try this.
pawn Код:
stock GetRandomPlayer()
{
new
playerid = random(MAX_PLAYERS);
if (!IsPlayerConnected(playerid)) GetRandomPlayer(); // call the function again - recursion
else return playerid;
}
Re: Random ID's -
MP2 - 26.11.2011
This (IMO) is a better way:
pawn Код:
#define MAX_PLAY 32 // Slots on your server
stock RandPlayer() // Will return a random player ID
{
new player[MAX_PLAY]; // The ID assigned to each slot
new plyrs; // Possible random players
for(new i=0; i<MAX_PLAY; i++)
{
if(IsPlayerConnected(i))
{
player[plyrs] = i;
plyrs++;
}
}
if(!plyrs) return -1; // If there aren't any players return -1
new rand;
rand = random(plyrs);
return player[rand];
}
With your method, if MAX_PLAYERS is 500 and there are only 5 players on you do realise it could take a while before it finds a connected player?
Re: Random ID's -
sleepysnowflake - 26.11.2011
What about
:
pawn Код:
new pRand = random(MAX_PLAYERS);
SendClientMessage(pRand,0xFFFFFF,"Congratulation, you have been RANDOMLY elected to be the server president!");
Re: Random ID's -
Cameltoe - 26.11.2011
Quote:
Originally Posted by Berlovan
What about :
pawn Код:
new pRand = random(MAX_PLAYERS); SendClientMessage(pRand,0xFFFFFF,"Congratulation, you have been RANDOMLY elected to be the server president!");
|
And if the random integer wouldn't be online then ?
Re: Random ID's -
MP2 - 26.11.2011
Then you would have to do
pawn Код:
while(!IsPlayerConnected(randplayer)) randplayer = random(MAX_PLAYERS);
As I said above, MAX_PLAYERS is 500 and it could take a while for it to find a connected player. It's totally un-efficient.
Re: Random ID's -
sleepysnowflake - 26.11.2011
Yes, because every player should optimize his script with 4 simple lines:
pawn Код:
#if defined MAX_PLAYERS
#undef MAX_PLAYERS
#define MAX_PLAYERS 30
#endif
Re: Random ID's -
MP2 - 26.11.2011
But that could still be a problem. Random is just that, RANDOM. It could go through hundreds, hell thousands of numbers before it found a connected player. It just isn't efficient.