[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
#2

Very Usefull stuff. Good job
Reply
#3

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

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

Nice work! Keep going..
Reply
#6

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

thanks!
Reply
#8

Will be useful, good job.
Reply
#9

Well nice going Jarnu .
Reply
#10

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

Nice as Always, good job
Reply
#12

Nice Tutorial
Reply
#13

Doesnt work for me! Anyone tested before posting?
Reply
#14

Updated, now test. It works perfect for me.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)