SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help

Reply
 
Thread Tools Display Modes
Old 16/09/2018, 01:23 AM   #1
SapMan
Banned
 
Join Date: Jul 2017
Posts: 140
Reputation: 12
Default select player random

How can I do if I want to randomly select one of the many players in the event? That is, of all those who are in the event, one is chosen.

Example: "Tester is now the killer of the event"
SapMan is offline   Reply With Quote
Old 16/09/2018, 02:21 AM   #2
SyS
High-roller
 
SyS's Avatar
 
Join Date: Oct 2015
Posts: 1,939
Reputation: 504
Default Re: select player random

You can make use of iterators in y_iterate.
PHP Code:
new Iterator:PlayersInEvent<MAX_PLAYERS>; 
When player enters event use Iter_Add
PHP Code:
Iter_Add(PlayersInEvent,playerid); 
Now for selecting random ids use Iter_Random
PHP Code:
new randomid Iter_Random(PlayersInEvent); 
When player leaves event use Iter_Remove
PHP Code:
Iter_Remove(PlayersInEvent,playerid); 
When event ends clear iterator using Iter_Clear
PHP Code:
Iter_Clear(PlayersInEvent); 
SyS is offline   Reply With Quote
Old 20/09/2018, 10:35 PM   #3
TheToretto
Gangsta
 
TheToretto's Avatar
 
Join Date: Jul 2015
Posts: 584
Reputation: 66
Default Re: select player random

Use a for loop

PHP Code:
for(new iMAX_PLAYERSi++)
{
    if(
//a boolean variable which is assigned to the playerid who joins the event. ex: InEvent[i]) 
    
{
        
// ur code here
    
}

TheToretto is offline   Reply With Quote
Old 20/09/2018, 10:42 PM   #4
KinderClans
Banned
 
Join Date: May 2018
Location: Italy
Posts: 716
Reputation: 43
Default Re: select player random

@TheToretto it's funny how you mentioned almost every Iterator function but made a loop using for(new i; i < MAX_PLAYERS; i++) ..
KinderClans is offline   Reply With Quote
Old 20/09/2018, 10:47 PM   #5
TheToretto
Gangsta
 
TheToretto's Avatar
 
Join Date: Jul 2015
Posts: 584
Reputation: 66
Default Re: select player random

Quote:
Originally Posted by KinderClans View Post
@TheToretto it's funny how you mentioned almost every Iterator function but made a loop using for(new i; i < MAX_PLAYERS; i++) ..

It's funny how you actually can't read users names. (if you still don't get it, it wasn't me who mentioned the Iterators funcs ;-; )
TheToretto is offline   Reply With Quote
Old 20/09/2018, 10:51 PM   #6
KinderClans
Banned
 
Join Date: May 2018
Location: Italy
Posts: 716
Reputation: 43
Default Re: select player random

Ah my bad then, after 5 hours of scripting i start to see ghosts.

I thought was you who mentioned all iterator functions.
KinderClans is offline   Reply With Quote
Old 20/09/2018, 11:14 PM   #7
NaS
High-roller
 
NaS's Avatar
 
Join Date: Mar 2008
Location: 🇩🇪
Posts: 1,786
Reputation: 635
Default Re: select player random

Quote:
Originally Posted by SapMan View Post
All right. Can you explain how I can do it in a way that protects the president? I mean to elect the president and the vice president randomly.
Code:
new president = Iter_Random(EventPlayers), vice_president;

do
{
vice_president = Iter_Random(EventPlayers);
}
while(president == vice_president);
This will select a random president first, then a vice president. In case they are the same, it will re-select a vice president until they aren't the same anymore.

Make sure that there are at least 2 players, otherwise it will be an endless loop.
NaS is offline   Reply With Quote
Old 22/09/2018, 03:41 AM   #8
SyS
High-roller
 
SyS's Avatar
 
Join Date: Oct 2015
Posts: 1,939
Reputation: 504
Default Re: select player random

The above method would be simple and will work but relying on random data to exit a iterative statement is a bad practice.You can remove random value from the iterator,assign it to a variable denoting president and again take the random value from iterator for vice president and add the value that was removed previously, back into the iterator.Also you can take the number of players in event using Iter_Count function.
PHP Code:
if(Iter_Count(PlayersInEvent) > 2){
    new 
president Iter_Random(PlayersInEvent);  
    
Iter_Remove(PlayersInEvent,president);
    new 
vice_president Iter_Random(PlayersInEvent);
    
Iter_Add(PlayersInEvent,president);
}else{
    
Iter_Clear(PlayersInEvent);
    
//end event ...

SyS is offline   Reply With Quote
Old 22/09/2018, 03:41 PM   #9
SapMan
Banned
 
Join Date: Jul 2017
Posts: 140
Reputation: 12
Default Re: select player random

Quote:
Originally Posted by SyS View Post
The above method would be simple and will work but relying on random data to exit a iterative statement is a bad practice.You can remove random value from the iterator,assign it to a variable denoting president and again take the random value from iterator for vice president and add the value that was removed previously, back into the iterator.Also you can take the number of players in event using Iter_Count function.
PHP Code:
if(Iter_Count(PlayersInEvent) > 2){
    new 
president Iter_Random(PlayersInEvent);  
    
Iter_Remove(PlayersInEvent,president);
    new 
vice_president Iter_Random(PlayersInEvent);
    
Iter_Add(PlayersInEvent,president);
}else{
    
Iter_Clear(PlayersInEvent);
    
//end event ...

I currently have it like this, is it wrong?

PHP Code:
P_INFO[TIME_START] --;
    if(
P_INFO[TIME_START] >= 1
    {
        for(new 
playerid 0GetPlayerPoolSize(); playerid <= jplayerid++)
        {
            if(
IsPlayerConnected(playerid))
            {
                if(
Player[playerid][M_PTP] == 1)
                {
                    new 
str[10]; format(strsizeof(str), "%d"P_INFO[TIME_START]);
                    
GameTextForPlayer(playeridstr10003);
                
                    new 
Iter_Random(PlayersInEvent);
                    if(!
P_INFO[PRESIDENT_READY])
                    {
                        
Iter_Remove(PlayersInEventr);
                        
P_INFO[PRESIDENT_READY] = true;
                        
Player[r][PLAYER_SERVICE_SECRET] = PRESIDENT;
                        
SendClientMessageEx(playerid, -1"%s is the new president"Player[r][NAME]);
                    }
                    else
                    {
                        
Player[r][PLAYER_SERVICE_SECRET] = VICE_PRESIDENT;
                        
SendClientMessageEx(playerid, -1"%s is the new vice president"Player[r][NAME]);
                    }
                }
            }
        }
    } 
SapMan is offline   Reply With Quote
Old 25/09/2018, 04:31 AM   #10
SapMan
Banned
 
Join Date: Jul 2017
Posts: 140
Reputation: 12
Default Re: select player random

I really need it. I need to make him only choose the president and the vice president only once. In the countdown, he chooses the president and the vice president at every moment.

CODE:

PHP Code:
P_INFO[TIME_START] --; 
    if(
P_INFO[TIME_START] >= 1)  
    { 
        for(new 
playerid 0GetPlayerPoolSize(); playerid <= jplayerid++) 
        { 
            if(
IsPlayerConnected(playerid)) 
            { 
                if(
Player[playerid][M_PTP] == 1
                { 
                    new 
str[10]; format(strsizeof(str), "%d"P_INFO[TIME_START]); 
                    
GameTextForPlayer(playeridstr10003); 
                 
                    new 
Iter_Random(PlayersInEvent); 
                    if(!
P_INFO[PRESIDENT_READY]) 
                    { 
                        
Iter_Remove(PlayersInEventr); 
                        
P_INFO[PRESIDENT_READY] = true
                        
Player[r][PLAYER_SERVICE_SECRET] = PRESIDENT
                        
SendClientMessageEx(playerid, -1"%s is the new president"Player[r][NAME]); 
                    } 
                    else 
                    { 
                        
Player[r][PLAYER_SERVICE_SECRET] = VICE_PRESIDENT
                        
SendClientMessageEx(playerid, -1"%s is the new vice president"Player[r][NAME]); 
                    } 
                } 
            } 
        } 
    } 
I need to have him only choose the president and the vice president once, and only show the countdown and not the message again. Any idea how to do that?
SapMan is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Select random KinderClans Scripting Help 15 20/08/2018 12:49 PM
foreach (y_iterate) - select random player from the same team. Arthurillo Scripting Help 7 10/09/2016 01:48 AM
Random player select VoViNaToR Help Archive 6 14/06/2011 07:22 PM
Random Player select Jay. Help Archive 13 17/04/2011 04:33 PM
random select player = <?> hipy Help Archive 6 22/07/2009 10:43 AM


All times are GMT. The time now is 06:57 PM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.