Burgershot
  • Home
  • Members
  • Team
  • Help
  • Search
  • Register
  • Login
  • Home
  • Members
  • Help
  • Search
Burgershot SA-MP Pawn Scripting [Pawn] Random Players

 
  • 0 Vote(s) - 0 Average
Pawn Random Players
Pr0fit
Offline

Burgershot Member
Posts: 3
Threads: 1
Joined: Oct 2020
Reputation: 0
Location: Private
#1
2020-10-25, 01:02 PM
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!
Jonies
Offline

Burgershot Member
Posts: 13
Threads: 5
Joined: Sep 2020
Reputation: 0
Location: Indonesia
#2
2020-10-25, 01:27 PM (This post was last modified: 2020-10-25, 01:28 PM by Jonies.)
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++;
}
<removed giant sig>
Josh
Offline

Administrator

Posts: 129
Threads: 1
Joined: Feb 2019
Reputation: 20
#3
2020-10-25, 03:19 PM
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.
Expert*
Offline

Burgershot Member
Posts: 61
Threads: 2
Joined: Apr 2019
Reputation: 4
#4
2020-10-25, 04:13 PM
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 ?
Pr0fit
Offline

Burgershot Member
Posts: 3
Threads: 1
Joined: Oct 2020
Reputation: 0
Location: Private
#5
2020-10-25, 05:59 PM
(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!
Pinch
Offline

Burgershot Member
Posts: 391
Threads: 19
Joined: Apr 2019
Reputation: 22
Location: Belgrade, Serbia
#6
2020-10-25, 07:56 PM (This post was last modified: 2020-10-25, 08:25 PM by Pinch.)
(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; ++i)
        {
            if(tmpIDs[i] == val)
                break;
                
            if(i == MAX_RND_PLAYERS)
            {
                tmpIDs[counter] = val;
                counter ++;
            }
        }
    }
    
    for(new id = 0; id < MAX_RND_PLAYERS; ++id)
    {
        // code
        // usage ex
        printf("ID: %i", tmpIDs[id]);
    }
    return 1;
}

This should work
Using Pawn.CMD?
If you're doing so, this is the very first sign that you absolutely shouldn't utilize your all powerful P-Code knowledge in any of the scripting discussion topics.
Pr0fit
Offline

Burgershot Member
Posts: 3
Threads: 1
Joined: Oct 2020
Reputation: 0
Location: Private
#7
2020-10-25, 08:50 PM
(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; ++i)
        {
            if(tmpIDs[i] == val)
                break;
               
            if(i == MAX_RND_PLAYERS)
            {
                tmpIDs[counter] = val;
                counter ++;
            }
        }
    }
   
    for(new id = 0; id < MAX_RND_PLAYERS; ++id)
    {
        // code
        // usage ex
        printf("ID: %i", tmpIDs[id]);
    }
    return 1;
}

This should work
I will give it a try tomorrow. Thank you so much! :)
« Next Oldest | Next Newest »



  • View a Printable Version
  • Subscribe to this thread
Forum Jump:

© Burgershot - Powered by our Community and MyBB Original Theme by Emerald

Linear Mode
Threaded Mode