[Tutorial] Creating a team balancer
#1

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.
Reply


Messages In This Thread
Creating a team balancer - by Jarnu - 06.12.2012, 05:02
Re: Creating a team balancer - by Samp_India - 06.12.2012, 05:30
Re: Creating a team balancer - by Trollolollo - 06.12.2012, 05:42
Re: Creating a team balancer - by RajatPawar - 06.12.2012, 05:46
Re: Creating a team balancer - by XtremeR - 06.12.2012, 06:01
Re: Creating a team balancer - by SlonCHL - 16.12.2012, 07:53
Re: Creating a team balancer - by xMCx - 16.12.2012, 11:39
Re: Creating a team balancer - by gtakillerIV - 16.12.2012, 12:22
Re: Creating a team balancer - by RiyadD - 16.12.2012, 13:42
Re: Creating a team balancer - by Lordzy - 17.12.2012, 10:21
Re: Creating a team balancer - by Lewis_Johnson - 18.12.2012, 15:00
Re: Creating a team balancer - by Lapon - 21.12.2012, 04:39
Re: Creating a team balancer - by [rG]Cold - 21.12.2012, 18:46
Re: Creating a team balancer - by Jarnu - 15.01.2013, 07:17

Forum Jump:


Users browsing this thread: 1 Guest(s)