Get online players ID
#1

Hello, is there any way to get the online players IDs?
Example:
I have connected 5 players, they're IDs are: 5, 6, 7, 19 and 39.
How can I get randomly, one of those IDs?
Reply
#2

Quote:
Originally Posted by ******
Посмотреть сообщение
pawn Код:
new rnd = Iter_Rand(Player);
Using y_iterate (foreach).
same
Reply
#3

Y_iterate (foreach) has a function for this.
Quote:
Originally Posted by foreach - 0.3 compatible - updated 19/12/12
Iter_Random(name)

Efficiently returns a random present value from the named iterator. Useful for selecting a random player.

Код:
new
	Iterator:Some<10>;
Iter_Add(Some, 5);
Iter_Add(Some, 9);
printf("%d", Iter_Random(Some));
Will print either "5" or "9".
Reply
#4

You can also do it with plain pawn functions.

You can create an array with the size of MAX_PLAYERS.
Then loop through all players and check if it's connected.
In case that ID is connected, add it to the array and count the connected players.
The you can get a random player from that array, using the count as random value.

pawn Код:
GetRandomConnectedPlayer()
{
    // Setup local variables
    new PlayerList[MAX_PLAYERS], Count;

    // Loop through all players
    for (new i; i < MAX_PLAYERS; i++)
    {
        // If this player is connected
        if (IsPlayerConnected(i))
        {
            // Add him to the player-list and increase the count
            PlayerList[Count] = i;
            Count++;
        }
    }

    // Select a random player from the connected players stored in the list and return it
    return PlayerList[random(Count)];
}
Usage:
pawn Код:
RandomPlayer = GetRandomConnectedPlayer();
Reply
#5

Quote:
Originally Posted by PowerPC603
Посмотреть сообщение
You can also do it with plain pawn functions.

You can create an array with the size of MAX_PLAYERS.
Then loop through all players and check if it's connected.
In case that ID is connected, add it to the array and count the connected players.
The you can get a random player from that array, using the count as random value.

pawn Код:
GetRandomConnectedPlayer()
{
    // Setup local variables
    new PlayerList[MAX_PLAYERS], Count;

    // Loop through all players
    for (new i; i < MAX_PLAYERS; i++)
    {
        // If this player is connected
        if (IsPlayerConnected(i))
        {
            // Add him to the player-list and increase the count
            PlayerList[Count] = i;
            Count++;
        }
    }

    // Select a random player from the connected players stored in the list and return it
    return PlayerList[random(Count)];
}
Usage:
pawn Код:
RandomPlayer = GetRandomConnectedPlayer();
This is the best way, i guess.. I did quite the same thing today with a script for my server ahahha
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)