select player random
#1

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"
Reply
#2

You can make use of iterators in y_iterate.
PHP код:
new Iterator:PlayersInEvent<MAX_PLAYERS>; 
When player enters event use Iter_Add
PHP код:
Iter_Add(PlayersInEvent,playerid); 
Now for selecting random ids use Iter_Random
PHP код:
new randomid Iter_Random(PlayersInEvent); 
When player leaves event use Iter_Remove
PHP код:
Iter_Remove(PlayersInEvent,playerid); 
When event ends clear iterator using Iter_Clear
PHP код:
Iter_Clear(PlayersInEvent); 
Reply
#3

Use a for loop

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

Reply
#4

@TheToretto it's funny how you mentioned almost every Iterator function but made a loop using for(new i; i < MAX_PLAYERS; i++) ..
Reply
#5

Quote:
Originally Posted by KinderClans
Посмотреть сообщение
@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 ;-; )
Reply
#6

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

I thought was you who mentioned all iterator functions.
Reply
#7

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 код:
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 ...

Reply
#8

Quote:
Originally Posted by SyS
Посмотреть сообщение
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 код:
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 код:
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]);
                    }
                }
            }
        }
    } 
Reply
#9

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 код:
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?
Reply
#10

If I understand, but I want to already choose the president and vice president, just continue the countdown and do not choose again. Is a timer that repeats every 1 second, then through that countdown is going to elect the president and the vice president, the problem is that it is choosing until the countdown is over and I do not want that, I want to choose a once..

I do not know if they understand me enough.
Reply
#11

Quote:
Originally Posted by SapMan
Посмотреть сообщение
If I understand, but I want to already choose the president and vice president, just continue the countdown and do not choose again. Is a timer that repeats every 1 second, then through that countdown is going to elect the president and the vice president, the problem is that it is choosing until the countdown is over and I do not want that, I want to choose a once..

I do not know if they understand me enough.
Do not place the code in the callback of timer then and especially inside a player loop as it will elect over and over again.

Elect the president and vice-president before the event starts and then set your timer to countdown.
Reply
#12

No, I think that where it is, it's fine. Because thanks to the timer he chooses the president and the vice president in the middle of the count.

I tried to do it that way (I have not tried it yet), but I do not know if it will work or it would be a good idea:

PHP код:
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)
                {
                    if(!
P_INFO[ALL_READY])
                    {
                        new 
president Iter_Random(PlayersInEvent);
                        
Iter_Remove(PlayersInEventpresident);
                        
Player[president][PLAYER_SERVICE_SECRET] = PRESIDENT;
                        
SendClientMessageEx(playerid, -1"%s is the new president"Player[president][NAME]);
                        
                        new 
vice_president Iter_Random(PlayersInEvent);
                        
Player[vice_president][PLAYER_SERVICE_SECRET] = VICE_PRESIDENT;
                        
SendClientMessageEx(playerid, -1"%s is the new vice president"Player[vice_president][NAME]);
                        
P_INFO[ALL_READY] = true;
                    }
                
                    new 
str[10]; format(strsizeof(str), "%d"P_INFO[TIME_START]);
                    
GameTextForPlayer(playeridstr10003);
                }
            }
        }
    } 
@Y_LESS, help me please, you have experience on this.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)