Random ID's
#1

How would I go about getting a random player using ID's?
Reply
#2

Try this.

pawn Код:
stock GetRandomPlayer()
{
    new
        playerid = random(MAX_PLAYERS);
    if (!IsPlayerConnected(playerid)) GetRandomPlayer(); // call the function again - recursion
    else return playerid;
}
Reply
#3

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?
Reply
#4

What about:

pawn Код:
new pRand = random(MAX_PLAYERS);
SendClientMessage(pRand,0xFFFFFF,"Congratulation, you have been RANDOMLY elected to be the server president!");
Reply
#5

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 ?
Reply
#6

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.
Reply
#7

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
Reply
#8

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.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)