Selecting random player per round
#1

Hi i find one stuff and its have one bug i will try explain it
so we have this code
Код:
stock Half()
{
	new Humans;
	foreach(new i : Player)
	{
	    if(Humans < 2)
	    {
	        HumanSetup(i);
	        printf("Selected humans");
	        Humans ++;
	    }
	    else
	    {
	        ZombieSetup2(i);
	        printf("Selected zombies");
	        Humans = 0;
	    }
	}
	printf("Finished Selecting teams");
	return 1;
}
So this code should select zombie and human players bat its have one bug and this bug work like this
we have 3players with names

Josh_Bla
Carl_Bla
Trump_Bla

So now always Trump_Bla will be zombie and Josh_Bla and Carl_Bla humans. So i think somehow i need make it random or something. Anyway i have no idea how should i make this random.
Reply
#2

PHP код:
//something like that
new Selected_Player = -1;
do{
    
Selected_Player random(GetPlayerPoolSize());
}while(!
IsPlayerConnected(Selected_Player));
//It will select a random playerid 
Reply
#3

Quote:
Originally Posted by Eoussama
Посмотреть сообщение
PHP код:
//something like that
new Selected_Player = -1;
do{
    
Selected_Player random(GetPlayerPoolSize());
}while(!
IsPlayerConnected(playerid))
//It will select a random playerid 
Well i dont rly get it.
Reply
#4

Quote:
Originally Posted by Pokemon64
Посмотреть сообщение
Well i dont rly get it.
if you are not familiar with random function, go see this
https://sampwiki.blast.hk/wiki/Random

OK now code explanation,
PHP код:
do{
    
//Your code here
}while(expression); 
Unlike for and while loop, do while loop checks the condition at the bottom expression, the loop will infinitely repeat unless the expression is false
In our case, we create a new variable, "Select_Player", then we assign a random value to it, "Select_Player = random(GetPlayerPoolSize())"
GetPlayerPoolSize returns the highest player ID connected, (for example, if the highest player ID on the server is 30, then GetPlayerPoolSize will return 30),
so Select_Player = random(GetPlayerPoolSize()), means that the Select_Player variable will have a random value between 0 and (The highest player Id connected to the server, it could be 30 or 23 or anything),
OK, now, let's say, random(GetPlayerPoolSize()) returns 12
here comes the role of while(expression) (in our case while(!IsPlayerConnected(Select_Player)), The randomely selected ID will be checked to see if there is a connected player with that ID, if there is, the loop will be stoped, if there isn't, the loop will continue on going (which means Select_Player variable will be reseted and a new value will be stored in it)

more about do while loop: https://sampwiki.blast.hk/wiki/While#do
Reply
#5

Quote:
Originally Posted by Eoussama
Посмотреть сообщение
if you are not familiar with random function, go see this
https://sampwiki.blast.hk/wiki/Random

OK now code explanation,
PHP код:
do{
    
//Your code here
}while(expression); 
Unlike for and while loop, do while loop checks the condition at the bottom expression, the loop will infinitely repeat unless the expression is false
In our case, we create a new variable, "Select_Player", then we assign a random value to it, "Select_Player = random(GetPlayerPoolSize())"
GetPlayerPoolSize returns the highest player ID connected, (for example, if the highest player ID on the server is 30, then GetPlayerPoolSize will return 30),
so Select_Player = random(GetPlayerPoolSize()), means that the Select_Player variable will have a random value between 0 and (The highest player Id connected to the server, it could be 30 or 23 or anything),
OK, now, let's say, random(GetPlayerPoolSize()) returns 12
here comes the role of while(expression) (in our case while(!IsPlayerConnected(Select_Player)), The randomely selected ID will be checked to see if there is a connected player with that ID, if there is, the loop will be stoped, if there isn't, the loop will continue on going (which means Select_Player variable will be reseted and a new value will be stored in it)

more about do while loop: https://sampwiki.blast.hk/wiki/While#do
Well i will try it. Thanks.
Reply
#6

Quote:
Originally Posted by Eoussama
Посмотреть сообщение
PHP код:
//something like that
new Selected_Player = -1;
do{
    
Selected_Player random(GetPlayerPoolSize());
}while(!
IsPlayerConnected(Selected_Player));
//It will select a random playerid 
Just use Iter_Random(Player) to return random values from Player iterator since he using foreach.
Reply
#7

Quote:
Originally Posted by Sreyas
Посмотреть сообщение
Just use Iter_Random(Player) to return random values from Player iterator since he using foreach.
So much opinions. What better and easer to do?
Reply
#8

Quote:
Originally Posted by Pokemon64
Посмотреть сообщение
So much opinions. What better and easer to do?
Iter_Random is better, especially when using foreach
Reply
#9

Quote:
Originally Posted by Eoussama
Посмотреть сообщение
Iter_Random is better, especially when using foreach
Yea i use foreach. Any explain with Iter_Random then?
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)