[Tutorial] Making a simple rank system for DM/TDM servers.
#1

Greetings.

This is a simple tutorial about how to create a rank system. It's mostly useful for DM/TDM servers.

I know there are other rank system too, but well this is my first tutorial on this forums so i hope you will understand the explanations.
So let's start.

First, we will need to include a_samp.inc as you know our script won't run without this include.

PHP код:
#include <a_samp> 
Now we will be defining the max ranks our server will have. This is required to hold the id for each ranks, example: Rank 1, 2, 3, and so on. For this tutorial we will be using 11 ranks (basically its 10 because rank 0 is not counted), you can change it to the max ranks you would like to have.

On the top of your script after includes;
PHP код:
#define MAX_RANKS           11 
We have our max ranks defined, now lets create an enumerator to hold the data for the rank.

PHP код:
enum E_RANKDATA
{
    
RankName[24], // This is to hold our rank name, for example: Privates, Corporal, Lieutenant etc...
    
RankScore// This one holds the score that is required to reach certain rank. For example: You will need 50 score to reach rank 1
    
FloatRankArmour // This is optional, i am using this for the tutorial only (Useful for TDM servers). You can ignore it if you want. This holds the amount of armour player should get at certain ranks.
    // For example: 20 percent armour at rank 2, 30 percent at rank 3 and so on...

Now we got our enumerator set, let's create a global variable to handle the data. This goes right underneath the enumerator;

PHP код:
new
    
g_RankInfo[MAX_RANKS][E_RANKDATA] = {
    
    
// Remember we had rank name, rank score and armour data saved in our enumerator? Those data will be held like this in the variable. You can change the scores, rank name to however you like.
    
    // Rank name                Score       Armour
    
{"Private",                 0,             0.0},
    {
"Corporal",                50,            10.0},
    {
"Sergeant",                100,        20.0},
    {
"Master Sergeant",            150,        30.0},
    {
"Lieutenant",                200,        40.0},
    {
"Major",                    500,        50.0},
    {
"Captain",                    1000,        60.0},
    {
"Brigadier",                1500,        70.0},
    {
"General",                    2500,        80.0},
    {
"Filed Marshal",            5000,        90.0},
    {
"Master of War",            10000,        100.0}
};
new 
Text3Dg_RankLabel[MAX_PLAYERS]; // This is a 3D text label to display the rank name on top of the player's head, we will be creating this later
new g_PlayerRank[MAX_PLAYERS]; // This holds the rank id for the player. 
We are finished defining the necessary data for our rank, now lets move to the other part.

We need to create a function that updates the player's rank. You can use it wherever you want, for example; if you make a command to display the player's current rank i.e /myrank, without updating the rank using this function will return the player's rank to 0.

Put this anywhere in your script;
PHP код:
// This function updates the player's rank. You can call this anywhere wherever you want to update the player's rank
UpdateRank(playerid)
{
    new
        
i;
    
    for (
MAX_RANKS 1>= 0i--)
    {
        if (
GetPlayerScore(playerid) >= g_RankInfo[i][RankScore])
        {
            break;
        }
    }
    
    
g_PlayerRank[playerid] = i;

Okay, now lets move to the callbacks

PHP код:
public OnPlayerConnect(playerid)
{
    
g_PlayerRank[playerid] = 0// Clearing the player rank to make sure other player that connects won't get the rank of the previous player that disconnected
    
g_RankLabel[playerid] = Create3DTextLabel(" "0xFFFFFF5530.040.050.030.001); // Creating the 3DText label
    
Attach3DTextLabelToPlayer(g_RankLabel[playerid], playerid0.00.00.5); // Attaching the text label to player, making the Z offset 0.5 will put the text on top of the player's head
    
return 1;
}
public 
OnPlayerDisconnect(playeridreason)
{
    
Delete3DTextLabel(g_RankLabel[playerid]); // Deleting the label so that it won't mix up with other player that connects with the same id
    
return 1;
}
public 
OnPlayerSpawn(playerid)
{
    
UpdateRank(playerid); // Updating player's rank to make sure g_PlayerRank variable has the player's current rank stored
    
new
        
string[64], // Just a string to send a message to let the player know what's his current rank is
        
rankstring[24], // String to fetch the rank name
        
rankid // To hold the player's rank id
    
;
    
rankid g_PlayerRank[playerid]; // Storing the player's rank into the rankid variable
    
format(rankstringsizeof(rankstring), "%s"g_RankInfo[rankid][RankName]); // Formatting the rankstring to store the player's rank
    
Update3DTextLabelText(g_RankLabel[playerid], GetPlayerColor(playerid), rankstring); // Adding the formatted rank name into the 3D text label with the same color that the player have
    
format(stringsizeof(string), "Your current rank is %i (%s) | Bonus armour: %0.2f"rankidg_RankInfo[rankid][RankName], g_RankInfo[rankid][RankArmour]); // Let the player know his current rank with how much bonus armour he can get
    
SendClientMessage(playerid, -1string);
    
SetPlayerArmour(playeridg_RankInfo[rankid][RankArmour]); // Setting armour depending on player's rank
    
return 1;

That's it.

I hope you learned something from this. And sorry if my explanations are too bad to be understood.
Reply
#2

Good work.

and make this
Код:
    format(string, sizeof(string), "Your current rank is %i (%s) | Bonus armour: %0.2F", rankid, g_RankInfo[rankid][RankName], g_RankInfo[rankid][RankArmour]); // Let the player know his current rank with how much bonus armour he can get
    SendClientMessage(playerid, -1, string);
To this
Код:
    format(string, sizeof(string), "Your current rank is %i (%s) | Bonus armour: %0.2f", rankid, g_RankInfo[rankid][RankName], g_RankInfo[rankid][RankArmour]); // Let the player know his current rank with how much bonus armour he can get
    SendClientMessage(playerid, -1, string);
maybe some people will report it as bug.

and why don't you update it when player get those amount of score that are needed for the new rank? its updating when the Player spawn.
Reply
#3

Quote:
Originally Posted by SpikY_
Посмотреть сообщение
Good work.

and make this
Код:
    format(string, sizeof(string), "Your current rank is %i (%s) | Bonus armour: %0.2F", rankid, g_RankInfo[rankid][RankName], g_RankInfo[rankid][RankArmour]); // Let the player know his current rank with how much bonus armour he can get
    SendClientMessage(playerid, -1, string);
To this
Код:
    format(string, sizeof(string), "Your current rank is %i (%s) | Bonus armour: %0.2f", rankid, g_RankInfo[rankid][RankName], g_RankInfo[rankid][RankArmour]); // Let the player know his current rank with how much bonus armour he can get
    SendClientMessage(playerid, -1, string);
maybe some people will report it as bug.

and why don't you update it when player get those amount of score that are needed for the new rank? its updating when the Player spawn.
Fixed the float specifier, just a small typing mistake, thanks for pointing it out.

And also i have already commented saying // This function updates the player's rank. You can call this anywhere wherever you want to update the player's rank so they can call the function when player's score gets updated, however thank you for your comment.
Reply
#4

good work !
Reply
#5

When player have a score around 50 then he got killed by enemy with two times or so. The current level would be decreased itself. (Private > Corporal > Private)
Reply
#6

Well, this is exactly how it should work. If you want to keep player's rank then you need to make a rank saving system using files or databases.

With this one, rank depends on scores that are defined in the array so rank increases/decreases when their score gets updated. This is not a bug or something, its made like that. Cause this rank system is based on DM/TDM servers as the title says.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)