01.09.2015, 15:30
(
Последний раз редактировалось Sjn; 07.09.2015 в 23:55.
)
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.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.
First, we will need to include a_samp.inc as you know our script won't run without this include.
PHP код:
#include <a_samp>
On the top of your script after includes;
PHP код:
#define MAX_RANKS 11
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
Float: RankArmour // 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...
}
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 Text3D: g_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 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 (i = MAX_RANKS - 1; i >= 0; i--)
{
if (GetPlayerScore(playerid) >= g_RankInfo[i][RankScore])
{
break;
}
}
g_PlayerRank[playerid] = i;
}
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(" ", 0xFFFFFF55, 30.0, 40.0, 50.0, 30.0, 0, 1); // Creating the 3DText label
Attach3DTextLabelToPlayer(g_RankLabel[playerid], playerid, 0.0, 0.0, 0.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(playerid, reason)
{
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(rankstring, sizeof(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(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);
SetPlayerArmour(playerid, g_RankInfo[rankid][RankArmour]); // Setting armour depending on player's rank
return 1;
}
I hope you learned something from this. And sorry if my explanations are too bad to be understood.