26.10.2012, 17:52
(
Последний раз редактировалось FireCat; 11.11.2012 в 17:43.
)
(banner by: Jansish)
Introduction
Welcome!
Well, yea I'm unbanned
So ahm... I'm going to do a tutorial, explaining how to do your own rank system!
Basically, ranks define what position you/others are compared to others.
So its easy to see who's the best at a certain skill.
Scripting
For this tutorial, I'm not going to require any "add-ons" such as sscanf and so.
So, I'm going to base this rank system, on scores.
This means, we will see if a person has a certain amount of score, making him rank up or not.
Lets make some defines!
pawn Код:
#define RANK_NEWBIE 0
#define RANK_COOL 1
#define RANK_OMGVAMPIRE 2
So like that, it will be easier to manage the ranks, instead of using 0, 1 and 2, we can simply use RANK_NEWBIE and such.
Q: Why are we using defines and not a normal variable?
A: Well, defines, most of the times is faster and easier to use.
Imagine, assigning a variable with another variable, would take more time than assigning a variable with the ... lets call it compiler language? Because using defines, we're messing around with raw information, causing it to change the compiler information, thats why when we're using macros (defines or other # information) its mainly faster than messing with variables.
Lets create the variables!
pawn Код:
new pRank[MAX_PLAYERS];
Instead of calling a function each time we want to know, that would include making calculations and such.
Under OnPlayerConnect put:
pawn Код:
pRank[playerid] = RANK_NEWBIE;
You can also use it under OnPlayerDisconnect, but IMHO its better to use it under OnPlayerConnect, but in this case it doesnt really matter, as we're assigning RANK_NEWBIE to 0, and since the pRank variable, when created, its being automatically assigned to 0, it doesn't really matter, but we'd assign RANK_NEWBIE to 1 and the player had never connected and we we're assigning the pRank variable to RANK_NEWBIE under OnPlayerDisconnect, some information could go wrong.
So IMHO, put it under OnPlayerConnect.
Lets make a function, thats calculates whats the rank the player is!
So, right in the bottom of your script, you should make something like this:
pawn Код:
forward CheckPlayerRank(playerid);
public CheckPlayerRank(playerid)
{
new iScore = GetPlayerScore(playerid);
switch(iScore)
{
case 0..100:
{
pRank[playerid] = RANK_NEWBIE;
//Rest of the information here.
}
case 101..200:
{
pRank[playerid] = RANK_COOL;
//Rest of the information here.
}
default:
{
pRank[playerid] = RANK_OMGVAMPIRE;
//Rest of the information here.
}
}
return 1;
}
After that, we're checking if the player's score is within one of the statements, that is:
If its within 0 and 100 it will set his rank to RANK_NEWBIE
If its within 101 and 200 it will set his rank to RANK_COOL
If its nether of those (higher or lower than those numbers) it will set his rank to RANK_OMGVAMPIRE.
Thats what default is for (mainly), if its none of the values checked, it will go to the default sub-function.
In theses cases, using switch is more... cleaner? Not faster nor more efficient, it just makes the script more organised, instead of doing if..else if... else if... else.
Get what I mean?
So, if you want to make this according to kills (if you have linked the kills variable to the score), you can just do something like:
pawn Код:
public OnPlayerDeath(playerid, killerid, reason)
{
SetPlayerScore(playerid, GetPlayerScore(playerid)-1);
CheckPlayerRank(playerid);
if(IsPlayerConnected(killerid))
{
SetPlayerScore(killerid, GetPlayerScore(killerid)+1);
CheckPlayerRank(killerid);
}
return 1;
}
After that, we're checking if the killerid is connect.
If the killerid was not connected (could be a death by collision), we're setting the score of a player that is not connected, and if you have crash detect it will appear an error.
So, after checking if the killerid is connected, we're setting giving him +1 score.
Then we're checking his rank.
If the killers id score would be 101 it would make his score to RANK_COOL.
You could make a textdraw showing the players rank!
The end
Well I hope you liked it!
If you have any questions/critics, be sure to post them here, so we can discuss about it.
jnfag pls