SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help > Tutorials

Reply
 
Thread Tools Display Modes
Old 04/04/2012, 10:12 AM   #1
[ABK]Antonio
High-roller
 
[ABK]Antonio's Avatar
 
Join Date: Sep 2011
Location: ID, USA
Posts: 1,223
Reputation: 187
Default Simple Team Setup + Player Data Template

Preface

Before we begin this tutorial, there's some things you should know. This doesn't cover how to actually save the data. Why it doesn't is simply because...Many people use different systems to save their player's data. There's also many, many tutorials based on those systems.
Some may not be able to follow my simplifications of certain things inside this tutorial.
You should know at LEAST...a few things...such as control structures. If you don't..don't worry, I'll cover a LITTLE of that. The reason I'm showing how to do player data structures is because..It's a LOT more useful than just a team based tutorial. It opens many doors to the scripter who learns it.

At the bottom there will be an example of the admin level system. If you have sscanf & zcmd and don't know how to make a setlevel command for example.. That will teach you how.

What You Will Learn
  • How to structure your players data
  • How to create multiple teams
  • How to use arrays and why to use arrays
What you MIGHT learn
  • How to do simple control structures



Player Data Structure

So we'll start of with our structure for the players data.

pawn Code:
enum PlayerInfo
{
    aLevel, //short for adminLevel (0)
    Kills, //1
    Deaths, //2
    Team //3
}

So what we have here is our enum. What does this do? Well, I'll have to show you the array to better explain.

pawn Code:
new Info[MAX_PLAYERS][PlayerInfo];

Right that's simple enough isn't it?
Let's think of this something like a box with slots in it...How many slots are in this box you ask? Well, we've defined it as MAX_PLAYERS. So by default there's 500 slots. So we have this box with 500 slots...We can put separate things in each one of those slots...We can ALSO put slots...INSIDE of slots! That's the basics of a 2-dimensional array. That's pretty easy to understand isn't it?

So when we want to put something in those slots...With our enum it makes things more readable and easier to change things in the long run.

Say I want to set the players Kills to 7...Well we'll do this
pawn Code:
Info[playerid][Kills] = 7;

That's pretty simple right? What is it actually doing though? Well, it's like taking a piece of paper...Putting it into the slot equivalent to the players ID...Then placing that piece of paper inside of our other slot which is Kills.

Let's say our players ID is 4
pawn Code:
Info[playerid][Kills] = 7;
Info[4][1] = 7; //it will be 1 instead of 2 as we start counting with 0 in computer language
That's basically what it's doing. Inserting something into these little slots in the box.

SO what is an enum? It's basically just a place-holder for these numbered slots (how we're using it here at least).

Now we should set their info to 0 when they connect.

pawn Code:
public OnPlayerConnect(playerid)
{
    Info[playerid][Kills] = 0; //We'll set all of our players things to 0 when they connect.
    Info[playerid][Deaths] = 0;
    Info[playerid][aLevel] = 0;
    return 1;
}

What can we do with this...I'm sure you've found some uses in your head, if not...Here's an example.

pawn Code:
public OnPlayerDeath(playerid, killerid, reason)
{
    //The != operator means NOT equal to.
    //This is one of the simplest forms of control structures.
    //INVALID_PLAYER_ID is a define inside of a_players (or a_samp) to detect, well if they're not a real player...Or not connected
    if(killerid != INVALID_PLAYER_ID)
    {
        //So if they ARE a valid player, let's continue our code.
        //Here we INCREMENT our player's Kills variable...or array slot.
        //What incrementing does is adds 1 to whatever the previous value is.
        //It's like doing...Info[killerid][Kills]+=1;
        Info[killerid][Kills]++;
    }
    //Even if the killer is an invalid player...We'll still give our player a death.
    Info[playerid][Deaths]++; //we increment again, this time using playerid instead of killerid (playerid = the one who died | killerid = the one who killed the player)

    //So basically, it will check our if statement... REGARDLESS of whether the player is a valid player we'll give the dead guy a death
    return 1;
}


Creating Teams

First, we'll start off by creating an enum.

pawn Code:
enum
{
    Grove,
    Ballas,
    Vagos,
    Nameless
}

As you've probably noticed..It's nameless..Why is that? Well, this way we don't need to define those numbers..It's much cleaner than doing this
pawn Code:
#define Grove   0
#define Ballas  1
#define Vagos   2
#define Namess  3

So.. How do we assign them to a team...Well, it's quite simple actually.

First we need to create our classes.
pawn Code:
public OnGameModeInit()
{
    //I'm not going to fill these in..If you want to know more about AddPlayerClass
    //Go here - [url]http://wiki.sa-mp.com/wiki/AddPlayerClass[/url]
    //=======[Grove]=======
    AddPlayerClass(105, x, y, z, angle, weapon1, ammo1, weapon2, ammo2, weapon3, ammo3); //0
    AddPlayerClass(106, x, y, z, angle, weapon1, ammo1, weapon2, ammo2, weapon3, ammo3); //1
    AddPlayerClass(107, x, y, z, angle, weapon1, ammo1, weapon2, ammo2, weapon3, ammo3); //2
    //=======[Ballas]======
    AddPlayerClass(102, x, y, z, angle, weapon1, ammo1, weapon2, ammo2, weapon3, ammo3); //3
    AddPlayerClass(103, x, y, z, angle, weapon1, ammo1, weapon2, ammo2, weapon3, ammo3); //4
    AddPlayerClass(104, x, y, z, angle, weapon1, ammo1, weapon2, ammo2, weapon3, ammo3); //5
    //=======[Vagos]=======
    AddPlayerClass(108, x, y, z, angle, weapon1, ammo1, weapon2, ammo2, weapon3, ammo3); //6
    AddPlayerClass(109, x, y, z, angle, weapon1, ammo1, weapon2, ammo2, weapon3, ammo3); //7
    AddPlayerClass(110, x, y, z, angle, weapon1, ammo1, weapon2, ammo2, weapon3, ammo3); //8
    //=====[Nameless]======
    AddPlayerClass(264, x, y, z, angle, weapon1, ammo1, weapon2, ammo2, weapon3, ammo3); //9
    AddPlayerClass(259, x, y, z, angle, weapon1, ammo1, weapon2, ammo2, weapon3, ammo3); //10
}

So we've got our classes..What do we do with them though? We do this...


pawn Code:
public OnPlayerRequestClass(playerid, classid)
{
    //We can set their camera positions and stuff..I'm not really going to get into that. Just the teams
   
    switch(classid) //A switch is basically an if statement on steroids.
    {
        case 0..2: Info[playerid][Team] = Grove; //If the class ID is 0, 1, or 2 this will set their team to grove.
        case 3..5: Info[playerid][Team] = Ballas;
        case 6..8: Info[playerid][Team] = Vagos;
        case 9, 10: Info[playerid][Team] = Nameless; //The comma is like...if the classid is 9 or 10.
    }
    //So we've set their teams now what do we do?
    //Let's actually create a function (just for easy reading)
    AssortTeam(playerid);
    return 1;
}

stock AssortTeam(playerid) //Please note that you can put this on the bottom of your script (very nice for organization)
{
    switch(Info[playerid][Team]) //We do another switch here
    {
        case Grove: SetPlayerTeam(playerid, Grove); //We then set their team corresponding to their team ID.
        case Ballas: SetPlayerTeam(playerid, Ballas);
        case Vagos: SetPlayerTeam(playerid, Vagos);
        case Nameless: SetPlayerTeam(playerid, NO_TEAM); //No team is a built in define that will make them teamless.
    }
}


So that's the basics of creating teams.


Learning More

If you've encountered a problem or anything...Feel free to post in this thread. If you find that something could be better in here, feel free to post too.

Here's that setlevel example.
pawn Code:
CMD:setlevel(playerid, params[])
{
    if(IsAdmin(playerid, 4)) //we check if they're an rcon admin or above level 4
    {
        new target, level; //create our variables to store our parameters in
        if(sscanf(params, "ri", target, level)) return SendClientMessage(playerid, 0xCC0000AA, "USAGE: /setlevel [playerid] [level]");
        if(level > 5) return SendClientMessage(playerid, 0xCC0000AA, "The maximum level is 5!"); //if it's above 5 it's a no no
        Info[target][aLevel] = level; //we set the level of our target to what the player enters
       
        new aName[MAX_PLAYER_NAME], tName[MAX_PLAYER_NAME]; //we create 2 string so we can store names in them.
        GetPlayerName(playerid, aName, sizeof(aName)); //we get the players name then store it
        GetPlayerName(target, tName, sizeof(tName)); //like above but for target
       
        new string[128]; //create a string so we can store our formatted message in it
        format(string, sizeof(string), "You set %s to admin level %d!", tName, level); //format our message...enables us to add the targets name & new level
        SendClientMessage(playerid, 0x00CCCCAA, string); //we send the string to the player
        format(string, sizeof(string), "%s set your admin level to %d!", aName, level); //like above
        SendClientMessage(target, 0x00CCCCAA, string); //we send it to the target
    }
    else return SendClientMessage(playerid, 0xCC0000AA, "You aren't the required admin level!");
    return 1;
}

stock IsAdmin(playerid, level) //What I usually do is something simple like this..just for ease of access for RCON admins.
{
    if(Info[playerid][aLevel] >= level || IsPlayerAdmin(playerid)) return true; //checks if they're level is greater than or equal to the input level, or if they're logged into rcon
    else return false; //returns true if they are, false if not.
}
the reason i created 2 name strings is for easier reading
__________________

ABKClan.com

Last edited by [ABK]Antonio; 11/04/2012 at 05:32 AM.
[ABK]Antonio is offline   Reply With Quote
Old 07/04/2012, 08:19 AM   #2
spedico
Huge Clucker
 
spedico's Avatar
 
Join Date: Jul 2010
Location: Finland
Posts: 237
Reputation: 35
Default Re: Simple Team Setup + Player Data Template

pawn Code:
public OnPlayerDeath(playerid, killerid, reason)
{
    //The != operator means NOT equal to.
    //This is one of the simplest forms of control structures.
    //INVALID_PLAYER_ID is a define inside of a_players (or a_samp) to detect, well if they're not a real player...Or not connected
    if(killerid != INVALID_PLAYER_ID)
    {
        //So if they ARE a valid player, let's continue our code.
        //Here we INCREMENT our player's Kills variable...or array slot.
        //What incrementing does is adds 1 to whatever the previous value is.
        //It's like doing...Info[playerid][Kills]+=1;
        Info[playerid][Kills]++;
    }
    //Even if the killer is an invalid player...We'll still give our player a death.
    Info[playerid][Deaths]++; //we increment again, this time using playerid instead of killerid (playerid = the one who died | killerid = the one who killed the player)

    //So basically, it will check our if statement... REGARDLESS of whether the player is a valid player we'll give the dead guy a death
    return 1;
}
You're giving the player who died a kill.. instead of "playerid" use "killerid"
spedico is offline   Reply With Quote
Old 07/04/2012, 09:08 AM   #3
DBan
Huge Clucker
 
Join Date: Jul 2010
Posts: 240
Reputation: 16
Default Re: Simple Team Setup + Player Data Template

Very good explanations +rep
DBan is offline   Reply With Quote
Old 07/04/2012, 09:10 AM   #4
Vukilore
Huge Clucker
 
Vukilore's Avatar
 
Join Date: Jan 2011
Location: http://forum.sa-mp-fr.com/index.php
Posts: 300
Reputation: 110
Default Re : Simple Team Setup + Player Data Template

Good tutorial for newbies
__________________
http://forum.sa-mp-fr.com/index.php. << here rest the french forum, in piece.
Vukilore is offline   Reply With Quote
Old 09/04/2012, 03:53 PM   #5
Jonny5
Gangsta
 
Join Date: Mar 2012
Location: Mi, USA
Posts: 929
Reputation: 158
Default Re: Simple Team Setup + Player Data Template

very well explained!! this will help alot of users!

9/10!!!
+rep
Jonny5 is offline   Reply With Quote
Old 11/05/2012, 04:28 PM   #6
Twisted_Insane
Gangsta
 
Twisted_Insane's Avatar
 
Join Date: Feb 2012
Posts: 975
Reputation: 95
Default Re: Simple Team Setup + Player Data Template

At least one good tutorial which is step-by-step 'n fairly explained. It'll be useful for sure...
__________________
Join the BEST Gangwar-server ever; Los Santos Gangwars!
http://forum.sa-mp.com/showthread.ph...42#post2235842
^ Read!

Any problems with ZCMD-commands? Simply PM me your topic or check out the tutorial(s) in my signature, more'll come soon!

Twisted_Insane is offline   Reply With Quote
Old 19/01/2013, 04:41 PM   #7
Sellize
Gangsta
 
Sellize's Avatar
 
Join Date: Jan 2013
Location: (ง ͠ ͟ل͜ ͡)ง fight me irl
Posts: 708
Reputation: 293
Default Re: Simple Team Setup + Player Data Template

Thanks! Good grammar and good explaining
__________________
dead
Sellize is offline   Reply With Quote
Old 20/01/2013, 10:23 AM   #8
Jear
Little Clucker
 
Join Date: Jan 2013
Posts: 24
Reputation: -2
Default Re: Simple Team Setup + Player Data Template

Thanks !
Jear is offline   Reply With Quote
Old 17/04/2013, 04:54 PM   #9
michaelcosyns
Big Clucker
 
Join Date: Mar 2012
Posts: 54
Reputation: 4
Default Re: Simple Team Setup + Player Data Template

Good tut. and very good explenation!
michaelcosyns is offline   Reply With Quote
Old 26/06/2020, 06:22 AM   #10
STORIEL000
Big Clucker
 
Join Date: Jun 2020
Location: Tree
Posts: 68
Reputation: 6
Default Re: Simple Team Setup + Player Data Template

And how do we invite a player to a team? I am new to scripting, and i became a developer on a friend's server. It's a roleplay, and we want to make teams like Paramedic, Police, Taxi ect. Could you please help me? ^_^
STORIEL000 is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Player data not loading celticfang Help Archive 8 12/08/2011 12:44 PM
SAVE PLAYER DATA Artix Help Archive 1 18/04/2011 11:50 PM
Looking For a Person To Setup his website with a template! RichardSteinheimer Everything and Nothing 18 04/01/2011 05:33 AM
getting player weapon data? 0ne Help Archive 0 02/05/2010 03:38 PM
Help - how to get data on the player in PHP X12X Help Archive 1 02/07/2009 02:03 PM


All times are GMT. The time now is 08:28 AM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.