[Tutorial] Setting up teams,team restricted vehicles
#1

Hello guys,

Here I come with just another tutorial ! Today, we will make a basic team script with team restricted vehicles. I don't want to talk too much. So let's start !

First of all, we need our defines and variables !

pawn Код:
//So let's start with defining our teams, so we can edit them soon easily !
   #define TEAM_RED  1
   #define TEAM_BLUE 2

//And let's continue with defining our teams' colors
  #define TEAM_REDCOLOR 0xFF0000AA // Defines Red Team's color
  #define TEAM_BLUECOLOR 0x0000FFAA // Defines Blue Team's color

  new gTeam[MAX_PLAYERS];//This is used for checking the player's team
  new redcar1; // We will use this soon to track if player's in red team's car or not
  new bluecar1; // We will use this soon to track if player's in blue team's car or not
So we are done with top of our script, but is it ended ? Of course not !

Now let's add player classes for each team, shall we ?

pawn Код:
// We will edit this soon aswell to track which team we will put our player at.
   AddPlayerClass(106,2522.7515,-1679.2373,15.4970,82.3750,0,0,0,0,0,0); // TEAM_RED's class,spawns at Grove St.
   AddPlayerClass(280,1547.0903,-1675.4006,13.9287,90.5706,0,0,0,0,0,0); // TEAM_BLUE's class,spawns at outside of PD.
Now search for this in your gamemode:

pawn Код:
public OnPlayerRequestClass(playerid, classid)
We will edit here to set the spawned player's team to a specific team. So let's go:

pawn Код:
public OnPlayerRequestClass(playerid, classid)
{
    SetPlayerTeamFromClass(playerid, classid); // We will define this function soon for setting the teams for each class.
    return 1;
}
So we implemented one custom function to our script and will have to define it soon. Is it a problem ? Of course not ! However, we will also have to set player's color to his team's color. First of all you will have to search for this in your gamemode:

pawn Код:
public OnPlayerSpawn(playerid)]
We will edit here to set the spawned player's color to a desired team's color. So let's go:

pawn Код:
public OnPlayerSpawn(playerid)
{
    SetPlayerToTeamColour(playerid); // We will define this function soon too for setting the player's color to desired team's color.
    return 1;
}
So we are done with everything to set a team up, but we have got 2 undefined functions and we must define them. So let's go ! :

pawn Код:
SetPlayerTeamFromClass(playerid, classid)
{
    if(classid == 0) // if the desired class is first class. PAWN and most other programming languages count 0 as first.
    {
        gTeam[playerid] = TEAM_RED; // Remember, we putted grove street as first, so if the player selects grove street skin, he will spawn as red team member.
    }
    else if(classid == 1) // else if the desired class is second class. As PAWN counts 0 as first, so 1 is the second skin.
    {
        gTeam[playerid] = TEAM_BLUE; // Remember, we putted cops as second, so if the player selects the cop skin, he will spawn as blue team member.
    }

}

SetPlayerToTeamColour(playerid)
{
    if(gTeam[playerid] == TEAM_BLUE) // if the player is a member of blue team
    {
        SetPlayerColor(playerid,TEAM_BLUECOLOR); // sets the player's color to blue.
    }
    else if(gTeam[playerid] == TEAM_RED) // if the player is a member of red team
    {
        SetPlayerColor(playerid,TEAM_REDCOLOR); // sets the player's color to red.
    }

}
You can add as more classes as you wish to, but as this is basic team script 2 will be enough for us. However, if you want to add more than one, you will have to know these mathematical operations:

Код:
&& means "and"
|| means "or"
For example, I want fifth classid and sixth classid to be in TEAM_BLUE, the correct code would be

pawn Код:
SetPlayerTeamFromClass(playerid, classid)
{
    if(classid == 4 || classid == 5) // As PAWN starts counting from 0, 4 is the fifth classid and 5 is the sixth. This means if classid is fifth OR classid is sixth, put them in TEAM BLUE !
    {
        gTeam[playerid] = TEAM_BLUE;
    }
    else if(classid == 1)
    {
        gTeam[playerid] = TEAM_RED;
    }

}
You can edit this team script for your needs. Now let's go for team restricted vehicles !

remember we defined our team cars with

pawn Код:
new redcar1; // We will use this soon to track if player's in red team's car or not
new bluecar1; // We will use this soon to track if player's in blue team's car or not
Now it is time to use them. Search for

pawn Код:
public OnGameModeInit()
in your pawno and create a vehicle, for me vehicles will be these:

pawn Код:
AddStaticVehicle(560,1534.8514,-1675.0812,13.0939,104.0228,95,83); //
AddStaticVehicle(560,1535.0137,-1697.9331,13.1682,178.7065,95,83); //
Both of them are sultans which spawn in front of the PD station in LS. Now we will have to define these cars as our team cars.

pawn Код:
redcar1 = AddStaticVehicle(560,1534.8514,-1675.0812,13.0939,104.0228,95,83); // Sets the redcar1 variable to be this vehicle
bluecar1 = AddStaticVehicle(560,1535.0137,-1697.9331,13.1682,178.7065,95,83); // Sets the bluecar1 variable to be this vehicle
So we are done with setting our team cars, now it is time for making them restricted ! For this, search for this in your pawno:

pawn Код:
public OnPlayerStateChange(playerid, newstate, oldstate)
We will edit here to get the player's state and check if he enters our redcar1 or bluecar1, so lets start:

Add this:
pawn Код:
if(newstate == PLAYER_STATE_DRIVER) // if the player's state is driver, which means if the player has turned his state into driving a car
{
   new carid = GetPlayerVehicleID(playerid); // Defines a new variable called "carid" and makes it get the player's vehicle ID.
   if(carid == bluecar1)// if the car's ID matches with bluecar1's.
   {
     if(gTeam[playerid] == TEAM_BLUE) // if player's team is blue.
     {
     // it won't do something as he matches the car's requirements.
     }
       else // if player's team is NOT blue
       {
         SendClientMessage(playerid,TEAM_REDCOLOR,"You must be in Blue Team to drive this car!"); // Sends a message to the player who tried driving the car with red color.
         RemovePlayerFromVehicle(playerid);//Removes the player from the vehicle.
       }
   }
   if(carid == redcar1) // if the car's ID matches with redcar1's
   {
     if(gTeam[playerid] == TEAM_RED) // if player's team is red.
     {
     // it won't do something as he matches the car's requirements.
     }
       else // if player's team is NOT red
       {
          SendClientMessage(playerid,TEAM_REDCOLOR,"You must be in Red Team to drive this car!"); // Sends a message to the player who tried driving the car with red color.
          RemovePlayerFromVehicle(playerid);//Removes the player from the vehicle.
       }
   }
}
So, that is all guys ! I appreciate any feedbacks ^^

Have fun !
Reply
#2

Please optimize...

pawn Код:
#define SetPlayerTeamFromClass(%1,%2) gTeam[%1] = (%2 == 0) ? (TEAM_BLUE) : (TEAM_RED)
#define SetPlayerToTeamColour(%1,%2) SetPlayerColor[%1] = (%2 == 0) ? (TEAM_BLUECOLOR) : (TEAM_REDCOLOR)
+3.
Reply
#3

They Should Be Defined Like This:
pawn Код:
forward Yourfunctionhere;
public yourfunctionhere
{
//Therestofyourcode
} //endhere
That Will Hardly Affect On The Tutorial.

I Say 5/10.
Reply
#4

@enzometic, thanks, i will change it when I am in computer, currently I am in phone ^^

@Youssef214, you say 5/10 just because I didnt put a statement that I dont have to ? Anyway thanks for the feedback.
Reply
#5

Quote:
Originally Posted by EnzoMetlc
Посмотреть сообщение
Please optimize...

pawn Код:
#define SetPlayerTeamFromClass(%1,%2) gTeam[%1] = (%2 == 0) ? (TEAM_BLUE) : (TEAM_RED)
#define SetPlayerToTeamColour(%1,%2) SetPlayerColor[%1] = (%2 == 0) ? (TEAM_BLUECOLOR) : (TEAM_REDCOLOR)
+3.
What is the obsession with macros? It's just easier to write a function for readability which is the case here, since it's a TUTORIAL. (Implying it's for aspiring and probably clueless scripters)
Reply
#6

WOW nice explain , this is a great Tutorial bud
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)