12.04.2013, 12:28
(
Last edited by Syntax; 28/04/2013 at 01:21 PM.
)
This kind of tutorial might have already been posted, but I am a little bit careless (sorry), as the only thing I am trying to do is help newbies create a basic team deathmatch script from scratch and at the same time, increase their skills massively. It is quite an in-depth tutorial, although there is not really much to explain If you have anything to say, say now, before it is too late. As a side note, I am thankful to all those people who have been helping me to understand some terms around SA-MP. I can't give names unfortunately.
The first thing you have to do is to make sure that at the top of your file the following line appears, otherwise your code will be full of errors, mainly because the group of functions that this include consists of will not be available.
The second step towards the finalisation of the script is to define the teams (defines are merely replacements for better understanding). So, at the top of your file (beneath the line from above), add the following lines:
Also, make sure you create an array to store the data of each player (considering that you have read tutorials about arrays and know how they work):
'g' stands for global for better recognition. It is global because it is not within any of the functions.
Note: 0x00FF00AA - is a hex color. The best way to get something similar to that is using the program GCP (Colorpicker tool) which can be easily downloaded using ****** or of course using Isolated's favourite, definitely one of the best sites in regards to colors.
The comments are unnecessary, but for a better organisation, you better let them be there.
The next thing we have to do is to add a class to class selection using AddPlayerClass on the OnGameModeInIt callback (this callback is triggered when the gamemode starts.)
You should have something like this within your script:
Note: In programming, the count starts from 0(nearly always, if not always).
On the callback OnPlayerRequestClass we add the following line:
Features
I've decided to show you how to add a basic scoring feature that is really important for TDM servers, otherwise your server will be plain and boring. So, please follow these easy steps, and you'll definitely get to grasp this newly shared knowledge.
Add the following enum at the top of your file (there is an in-depth tutorial about enums, but fundementally, it is a group of variables):
The next thing we have to do is to declare a multi-dimensional array:
COLOR_RED - you must have it defined, otherwise you'll get errors.
The first thing you have to do is to make sure that at the top of your file the following line appears, otherwise your code will be full of errors, mainly because the group of functions that this include consists of will not be available.
pawn Code:
#include <a_samp>
The second step towards the finalisation of the script is to define the teams (defines are merely replacements for better understanding). So, at the top of your file (beneath the line from above), add the following lines:
pawn Code:
/////// SYSTEM DEFINED TEAMS AND COLORS /////
#define TEAM_GROVE 1
#define TEAM_BALLAS 2
#define TEAM_GROVE_COLOR 0x00FF00AA
#define TEAM_BALLAS_COLOR 0xFF00FFAA
///////////////////////////////////////////////////////
pawn Code:
new gTeam[MAX_PLAYERS];
Note: 0x00FF00AA - is a hex color. The best way to get something similar to that is using the program GCP (Colorpicker tool) which can be easily downloaded using ****** or of course using Isolated's favourite, definitely one of the best sites in regards to colors.
The comments are unnecessary, but for a better organisation, you better let them be there.
The next thing we have to do is to add a class to class selection using AddPlayerClass on the OnGameModeInIt callback (this callback is triggered when the gamemode starts.)
You should have something like this within your script:
pawn Code:
public OnGameModeInit()
{
SetGameModeText("TDM");
AddPlayerClass(106,2431.3733,-1679.4271,13.7673,0,24,100,0,0,0,0); // classid 0 = TEAM_GROVE
AddPlayerClass(102,2167.8926,-1670.0753,15.0805,0,24,100,0,0,0,0); // classid 1 = TEAM_BALLAS
return 1;
}
On the callback OnPlayerRequestClass we add the following line:
pawn Code:
public OnPlayerRequestClass(playerid, classid) // this callback is triggered when the class selection menu appears.
{
SetPlayerTeamFromClass(playerid, classid);
}
pawn Code:
SetPlayerTeamFromClass(playerid, classid) // this is not a native function
{
switch(classid) // it switches through classids
{
case 0: // classid 0
{
gTeam[playerid] = TEAM_GROVE; // it sets the data of the player who selected the first team (in the gTeam array) to TEAM_GROVE
{
SetPlayerColor(playerid,TEAM_GROVE_COLOR);
}
}
case 1: // classid 1
{
gTeam[playerid] = TEAM_BALLAS; // it sets the data of the player who selected the second team (in the gTeam array) to TEAM_BALLAS
{
SetPlayerColor(playerid, TEAM_BALLAS_COLOR);
}
}
}
}
I've decided to show you how to add a basic scoring feature that is really important for TDM servers, otherwise your server will be plain and boring. So, please follow these easy steps, and you'll definitely get to grasp this newly shared knowledge.
Add the following enum at the top of your file (there is an in-depth tutorial about enums, but fundementally, it is a group of variables):
pawn Code:
enum pInfo //it creates an enum that is called pInfo with the following variables:
{
pScore,
pMoney
}
pawn Code:
new gPlayerData[MAX_PLAYERS][pInfo];
pawn Code:
public OnPlayerDeath(playerid, killerid, reason) // this callback is called when a player dies
{
if(gTeam[killerid] == gTeam[playerid]) // using an if-statement, we check if the team of the killer is same as the team of the player who died
{
SendClientMessage(killerid, COLOR_RED, "You have killed a teammate, resulting a loss of $1000 and 4 score."); // it sends a red coloured client message to the killer
gPlayerData[killerid][pMoney] -= 1000;
SetPlayerMoney(killerid, gPlayerData[killerid][pMoney];
SetPlayerScore(killerid, gPlayerData[killerid][pScore] - 4);
}
else if(gTeam[killerid] != gTeam[playerid]) // using an if-statement, we check if the team of the killer is not same as the team of player who died
{
gPlayerData[playerid][pMoney] -=100;
gPlayerData[killerid][pMoney] +=100;
SetPlayerMoney(playerid, gPlayerData[playerid][pMoney]);
SetPlayerMoney(killerid, gPlayerData[killerid][pMoney]);
SetPlayerScore(playerid, gPlayerData[playerid][pScore]-1);
SetPlayerScore(killerid, gPlayerData[killerid][pScore]+1);
}
SendDeathMessage(killerid, playerid, reason);
return 1;
}