SA-MP Forums Archive
Get online players ID - 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: Get online players ID (/showthread.php?tid=566243)



Get online players ID - awoo - 04.03.2015

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?


Re: Get online players ID - KayJ - 04.03.2015

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


Re: Get online players ID - CalvinC - 04.03.2015

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



Re: Get online players ID - PowerPC603 - 04.03.2015

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();



Re: Get online players ID - ReshiramZekrom - 04.03.2015

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