SA-MP Forums Archive
[Tutorial] Creating a team balancer - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+---- Forum: Tutorials (https://sampforum.blast.hk/forumdisplay.php?fid=70)
+---- Thread: [Tutorial] Creating a team balancer (/showthread.php?tid=397643)



Creating a team balancer - Jarnu - 06.12.2012

Starting.

Extra note -
__________________________________________________ ___________
Well, for the sake of tutorial i'll define 3 teams.

pawn Code:
#define TEAM1 0
#define TEAM2 1
#define TEAM3 2
Team variable

pawn Code:
new gTeam[MAX_PLAYERS];
OnPlayerRequestClass

-You've to do it according to your script.
pawn Code:
public OnPlayerRequestClass(playerid, classid)
{
     switch(classid)
     {
          case 0: { gTeam[playerid] = TEAM1; SetPlayerTeam(playerid, 0); }
          case 1: { gTeam[playerid] = TEAM2; SetPlayerTeam(playerid, 1); }
          case 2: { gTeam[playerid] = TEAM3; SetPlayerTeam(playerid, 2); }
     }
     return 1;
}
//REMEMBER: YOU HAVE TO USE IT ACCORDING TO YOUR SCRIPT THIS IS JUST AN EXAMPLE.
We will use the above definations in tutorial.

Remeber: If you already got team defines ignore this step.
__________________________________________________ ___________


Step1: Creating the function to check the player's count in a team.


We will create a simple stock/function that will get the player's count in a particular team.

pawn Code:
//Remeber you must have already defined teams.
stock GetPlayersCountInTeam(teamid) //This is our function "GetPlayersCountInTeam(teamid)"
{
     //We will create a variable here called "playercount" or you can define it as your own like "count" or something else you want.
     new playercount = 0; //We set it to '0' since we didn't counted any players yet.
     //Next is a 'for loop'.
     for(new x = 0; x < MAX_PLAYERS; x ++)
     {
           if(GetPlayerState(x) == PLAYER_STATE_NONE) continue; //If player is in class selection, continue.
           if(GetPlayerTeam(x) != teamid) continue; //If the player is not a specified team id. then continue;
           playercount++; //Else count the player in the team. (The team you will use in GetPlayersCountInTeam function)
     }
     return playercount; //Will return the players count in that team.
}
Extra: I used stock rather than public because with stock you can return a string, but a public function can't

Step2: Implementing the function we created.


Now, we will use the above function for restricting the player to join a team which has more player's than the other teams.

For that restriction we will use the call back OnPlayerRequestSpawn

pawn Code:
public OnPlayerRequestSpawn(playerid) //When the player clicks the SPAWN button
{
     new team1count = GetPlayersCountInTeam(TEAM1); //Team 1 count's variable
     new team2count = GetPlayersCountInTeam(TEAM2);  //Team 2 count's variable
     new team3count = GetPlayersCountInTeam(TEAM3);  //Team 3 count's variable
     switch(gTeam[playerid]) //We will use the switch case.
     {
          case TEAM1: //If player's team is TEAM1
          {
                if(team1count > team2count && team3count) //if the player's count of TEAM1 is > (greater) than the team 2's count and team 3's count then.
                {
                      SendClientMessage(playerid, 0xFF0000FF,"This team is currently full!");
                      return 0; //Stopping the player to Spawn
                }
          }
          case TEAM2: //If player's team is TEAM2
          {
                if(team2count > team1count && team3count) //if the player's count of TEAM2 is > (greater) than the team 1's count and team 3's count then.
                {
                      SendClientMessage(playerid, 0xFF0000FF,"This team is currently full!");
                      return 0; //Stopping the player to Spawn
                }
          }
          case TEAM3: //If player's team is TEAM3
          {
                if(team3count > team1count && team2count) //if the player's count of TEAM3 is > (greater) than the team 1's count and team 2's count then.
                {
                      SendClientMessage(playerid, 0xFF0000FF,"This team is currently full!");
                      return 0; //Stopping the player to Spawn
                }
          }
      }
      return 1; //Else spawn the player.
} //Ending bracket.

And with a 1 simple function we created a team balancer!


Thank you for reading the tutorial.


Re: Creating a team balancer - Samp_India - 06.12.2012

Very Usefull stuff. Good job


Re: Creating a team balancer - Trollolollo - 06.12.2012

SAMP INDIA CAN YOU HELP ME?
http://forum.sa-mp.com/showthread.ph...95#post2256895


Re: Creating a team balancer - RajatPawar - 06.12.2012

Nice one, making teams always fucks me up. Thanks!


Re: Creating a team balancer - XtremeR - 06.12.2012

Nice work! Keep going..


Re: Creating a team balancer - SlonCHL - 16.12.2012

OMFG Thanks dude, I was looking for how to Get a Team's players, Exactly what I needed


Re: Creating a team balancer - xMCx - 16.12.2012

thanks!


Re: Creating a team balancer - gtakillerIV - 16.12.2012

Will be useful, good job.


Re: Creating a team balancer - RiyadD - 16.12.2012

Well nice going Jarnu .


Re: Creating a team balancer - Lordzy - 17.12.2012

You must also tell to use 'SetPlayerTeam' when a player gets spawned or else it'll just create errors or mistakes.


Re: Creating a team balancer - Lewis_Johnson - 18.12.2012

Nice as Always, good job


Re: Creating a team balancer - Lapon - 21.12.2012

Nice Tutorial


Re: Creating a team balancer - [rG]Cold - 21.12.2012

Doesnt work for me! Anyone tested before posting?


Re: Creating a team balancer - Jarnu - 15.01.2013

Updated, now test. It works perfect for me.