[Tutorial] TDM Gamemode
#1

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.

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
///////////////////////////////////////////////////////
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):

pawn Code:
new gTeam[MAX_PLAYERS];
'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:

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;
}
Note: In programming, the count starts from 0(nearly always, if not always).

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);
            }
        }
    }
}
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):

pawn Code:
enum pInfo //it creates an enum that is called pInfo with the following variables:
{
    pScore,
    pMoney
}
The next thing we have to do is to declare a multi-dimensional array:

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;
}
COLOR_RED - you must have it defined, otherwise you'll get errors.
Reply
#2

include <a_samp> you forgot the Hash tag. Just a note, before you get spammed. I also recommend people use colorpicker.com, instead of downloading.
Reply
#3

Quote:
Originally Posted by MikeLovesToHelp
View Post
include <a_samp> you forgot the Hash tag. Just a note, before you get spammed. I also recommend people use colorpicker.com, instead of downloading.
I should've said that I was recommending it and thanks, I've just edited the thread.
Reply
#4

No problem dude. It's nice to see someone giving a simple idea across to new scripters, as all it seems to be these days is that forum users are spamming the forums with issues they could easily find. Also, it's nice that it's not complex nor to much in depth, as it's not really needed for it to be, but you've explained everything, and the point has been put across. Plus the layout is nice, and easily read. I actually read this tutorial, most of the time I just skip through it.
Reply
#5

Thanks for the kind words. I greatly appreciate it. Although I hate to say it, I am a newbie as well, but I am hoping that one day I'll help other people to climb towards the top of the 'pyramid' progressively. I am not a master, but I want to have my knowledge shared.
Reply
#6

Nice
Reply
#7

Quote:
Originally Posted by ElMelo
View Post
Nice
Thanks. Hope it helped you.
Reply
#8

Quote:
Originally Posted by Syntax
View Post
Thanks for the kind words. I greatly appreciate it. Although I hate to say it, I am a newbie as well, but I am hoping that one day I'll help other people to climb towards the top of the 'pyramid' progressively. I am not a master, but I want to have my knowledge shared.
Everyone starts somewhere, there is no shame in admitting to being a beginner, right now I'm attempting to hack ******' mind to drain all his knowledge, HE WILL GIVE IT TO ME!!!
Reply
#9

Quote:
Originally Posted by Isolated
View Post
Everyone starts somewhere, there is no shame in admitting to being a beginner, right now I'm attempting to hack ******' mind to drain all his knowledge, HE WILL GIVE IT TO ME!!!
I wish you good luck in at least attempting that.
Reply
#10

Quote:
Originally Posted by Syntax
View Post
I wish you good luck in at least attempting that.
everything is possible XD
Reply
#11

Nice tutorial.
Reply
#12

Awesome
Reply
#13

Thank you all.

LE: I am working on a script to release from which people can inspire.
Reply
#14

Has been updated.

(sorry for double posting)
Reply
#15

Syntax, you'll receive some errors when compiling. For example you typed TEAM_BALLAS but in the #define you typed TEAM_BALLA - I think you should test it over again, there are more errors. (You didn't define COLOR_RED.)

Thanks anyways for helping people.
Reply
#16

Oh, I am so sorry. It was just a spelling mistake. About the color, I have it defined in an include (Isolated, a very good friend of mine, did it for me), so I can't call it a personal mistake, but I'll edit my script right away.

Moreover, I'd like to thank you for noticing. That means that you have not just skimmed through the tutorial, but read it, therefore your support is greatly appreciated. In recognition, I've repped you positively.

Should you have questions about this tutorial overall, feel free to private message me.
Reply
#17

pawn Code:
#define COLOR_RED 0xFF0000AA // Put at the top of the TDM script. Then Syntax doesn't get mad.
Reply
#18

Quote:
Originally Posted by Isolated
View Post
pawn Code:
#define COLOR_RED 0xFF0000AA // Put at the top of the TDM script. Then Syntax doesn't get mad.
Canada omw LOL
Reply
#19

dont know if its old or not(i didnt wanna revive) but it seems if you add another class it give this error
Quote:

C:\games\GTA SanAndreas\SeRveR SaMp\gamemodes\my own script.pwn(89) : warning 217: loose indentation
C:\games\GTA SanAndreas\SeRveR SaMp\gamemodes\my own script.pwn(89) : error 014: invalid statement; not in switch
C:\games\GTA SanAndreas\SeRveR SaMp\gamemodes\my own script.pwn(89) : warning 215: expression has no effect
C:\games\GTA SanAndreas\SeRveR SaMp\gamemodes\my own script.pwn(89) : error 001: expected token: ";", but found ":"
C:\games\GTA SanAndreas\SeRveR SaMp\gamemodes\my own script.pwn(89) : error 029: invalid expression, assumed zero
C:\games\GTA SanAndreas\SeRveR SaMp\gamemodes\my own script.pwn(89) : fatal error 107: too many error messages on one line

Compilation aborted.Pawn compiler 3.2.3664 Copyright © 1997-2006, ITB CompuPhase


4 Errors.

Reply
#20

Quote:
Originally Posted by mubashir
View Post
dont know if its old or not(i didnt wanna revive) but it seems if you add another class it give this error
pawn Code:
C:\games\GTA SanAndreas\SeRveR SaMp\gamemodes\my own script.pwn(89) : warning 217: loose indentation
C:\games\GTA SanAndreas\SeRveR SaMp\gamemodes\my own script.pwn(89) : error 014: invalid statement; not in switch
C:\games\GTA SanAndreas\SeRveR SaMp\gamemodes\my own script.pwn(89) : warning 215: expression has no effect
C:\games\GTA SanAndreas\SeRveR SaMp\gamemodes\my own script.pwn(89) : error 001: expected token: ";", but found ":"
C:\games\GTA SanAndreas\SeRveR SaMp\gamemodes\my own script.pwn(89) : error 029: invalid expression, assumed zero
C:\games\GTA SanAndreas\SeRveR SaMp\gamemodes\my own script.pwn(89) : fatal error 107: too many error messages on one line

Compilation aborted.Pawn compiler 3.2.3664 Copyright (c) 1997-2006, ITB CompuPhase


4 Errors.
That error is not because you added another class. It's most likely you missed a bracket on the previous line or did something wrong on that line. Check the code carefully and you should see it
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)