open.mp forum
[Pawn] Random Players - Printable Version

+ open.mp forum (https://forum.open.mp)
-- Forum: SA-MP (https://forum.open.mp/forumdisplay.php?fid=3)
--- Forum: Pawn Scripting (https://forum.open.mp/forumdisplay.php?fid=10)
--- Thread: [Pawn] Random Players (/showthread.php?tid=1278)



Random Players - Pr0fit - 2020-10-25

Hey there, I'm new to scripting, so I was wondering if anyone could help me, please.



Basically, I would need a script that would randomly select 5 players, out of all players on the server.



In the script, selected?players shouldn't repeat. I have 0 idea of how I could do it, so if anyone has any time to spare and help me out, it would be awesome.



Thank you!


RE: Random Players - Jonies - 2020-10-25

new UsedID[MAX_PLAYERS];
new counter;

foreach(new i : Player)
{
if(counter >= 5) break;
new RandomID = random(i);
if(!IsPlayerConnected(i));
UsedID[RandomID] = 1;
counter;
}


RE: Random Players - Josh - 2020-10-25

You can use y_foreach and use the Iter_Random function.

https://github.com/pawn-lang/YSI-Includes/blob/5.x/YSI_Data/y_foreach/api.md

search Iter_Random, it'll give you options of exclusions so you can include the already random players.


RE: Random Players - Expert* - 2020-10-25

I can make a function for that with no extra includes. YSI is amazing but if you dont want to use it it's up to you.



1. What if there is only 5 online. We fail the function or pick them ?

2 What if there is only 1 or 2 people online, do we pick them or fail the function ?


RE: Random Players - Pr0fit - 2020-10-25

(2020-10-25, 04:13 PM)Expert* Wrote: I can make a function for that with no extra includes. YSI is amazing but if you dont want to use it it's up to you.



1. What if there is only 5 online. We fail the function or pick them ?

2 What if there is only 1 or 2 people online, do we pick them or fail the function ?



Oh, that would be lovely if you can, since I don't understand much in coding.



Well, if possible, if there are 10 people, the function would only work then.



Also, players shouldn't repeat in that function, if that's possible as well.



And thank you, and everyone else for answering!


RE: Random Players - Pinch - 2020-10-25

(2020-10-25, 05:59 PM)Pr0fit Wrote:
(2020-10-25, 04:13 PM)Expert* Wrote: I can make a function for that with no extra includes. YSI is amazing but if you dont want to use it it's up to you.

1. What if there is only 5 online. We fail the function or pick them ?
2 What if there is only 1 or 2 people online, do we pick them or fail the function ?

Oh, that would be lovely if you can, since I don't understand much in coding.

Well, if possible, if there are 10 people, the function would only work then.

Also, players shouldn't repeat in that function, if that's possible as well.

And thank you, and everyone else for answering!
I'm on it, I'll edit the comment once I finish writing the function as I'm writing on my android

Code:
randomPlayerFunction()
{
    const
        MAX_RND_PLAYERS = 5,
        MIN_RND_ONLINE = 10;
        
    if (Iter_Count(Player) < MIN_RND_ONLINE) return 0;
    
    new
        tmpIDs[MAX_RND_PLAYERS] = INVALID_PLAYER_ID,
        counter;
        
    while(tmpIDs[MAX_RND_PLAYERS - 1] == INVALID_PLAYER_ID)
    {
        new
            val = Iter_Random(Player);
            
        for(new i = 0; i < MAX_RND_PLAYERS; )
        {
            if(tmpIDs[i] == val)
                break;
                
            if(i == MAX_RND_PLAYERS)
            {
                tmpIDs[counter] = val;
                counter ;
            }
        }
    }
    
    for(new id = 0; id < MAX_RND_PLAYERS; 﨧)
    {
        // code
        // usage ex
        printf("ID: %i", tmpIDs[id]);
    }
    return 1;
}

This should work


RE: Random Players - Pr0fit - 2020-10-25

(2020-10-25, 07:56 PM)Pinch Wrote:
(2020-10-25, 05:59 PM)Pr0fit Wrote:
(2020-10-25, 04:13 PM)Expert* Wrote: I can make a function for that with no extra includes. YSI is amazing but if you dont want to use it it's up to you.



1. What if there is only 5 online. We fail the function or pick them ?

2 What if there is only 1 or 2 people online, do we pick them or fail the function ?



Oh, that would be lovely if you can, since I don't understand much in coding.



Well, if possible, if there are 10 people, the function would only work then.



Also, players shouldn't repeat in that function, if that's possible as well.



And thank you, and everyone else for answering!

I'm on it, I'll edit the comment once I finish writing the function as I'm writing on my android



Code:
randomPlayerFunction()

{

? ? const

? ? ? ? MAX_RND_PLAYERS = 5,

? ? ? ? MIN_RND_ONLINE = 10;

? ? ? ?

? ? if (Iter_Count(Player) < MIN_RND_ONLINE) return 0;

? ?

? ? new

? ? ? ? tmpIDs[MAX_RND_PLAYERS] = INVALID_PLAYER_ID,

? ? ? ? counter;

? ? ? ?

? ? while(tmpIDs[MAX_RND_PLAYERS - 1] == INVALID_PLAYER_ID)

? ? {

? ? ? ? new

? ? ? ? ? ? val = Iter_Random(Player);

? ? ? ? ? ?

? ? ? ? for(new i = 0; i < MAX_RND_PLAYERS; )

? ? ? ? {

? ? ? ? ? ? if(tmpIDs[i] == val)

? ? ? ? ? ? ? ? break;

? ? ? ? ? ? ? ?

? ? ? ? ? ? if(i == MAX_RND_PLAYERS)

? ? ? ? ? ? {

? ? ? ? ? ? ? ? tmpIDs[counter] = val;

? ? ? ? ? ? ? ? counter ;

? ? ? ? ? ? }

? ? ? ? }

? ? }

? ?

? ? for(new id = 0; id < MAX_RND_PLAYERS; 﨧)

? ? {

? ? ? ? // code

? ? ? ? // usage ex

? ? ? ? printf("ID: %i", tmpIDs[id]);

? ? }

? ? return 1;

}



This should work

I will give it a try tomorrow. Thank you so much! :)