[Tutorial] "Function" your variable-names - Make your life easier!
#1

(Note!: My mainlanguage is german.)

"Function" your variable-names - Make your life easier!

Because there are many guys who write there code like the example[1] below, i now want to tell you an easier way to done.

[1] the example - Actualy used.
PHP Code:
if(PlayerInfo[playerid][pAdmin] == 1)
{
    
SendClientMessage(playeridYOUR_COLOR"You`re: Moderator");
}
else if(
PlayerInfo[playerid][pAdmin] == 2)
{
    
SendClientMessage(playeridYOUR_COLOR"You`re: Supporter");
}
else if(
PlayerInfo[playerid][pAdmin] == 3)
{
    
SendClientMessage(playeridYOUR_COLOR"You`re: Administrator");
}
Result for Rank 2"You`re Supporter" 
You now always have to repeat this wherever you want to add the rankname. /ban command, /kick command, maybe a /say command and freaking everywhere, where you want to show the current rankname.


With a easy method you are able to reduce your needed code and to make your life easier. Just check the example[2]!

[2] the example - Should be used.
PHP Code:
GetPlayerAdminName(playerid)
{
    new 
result[14];
    switch(
PlayerInfo[playerid][pAdmin])
    {
        case 
0:{result="No Admin";}
        case 
1:{result="Moderator";}
        case 
2:{result="Supporter";}
        case 
3:{result="Administrator";}
    }
    return 
result;

By adding this in your script, you are able to use this everywhere. For some scripting examples watch the example[3].

[3] the example - How it works
PHP Code:
new string[64];
format(string,sizeof(string),"You`re: %s",GetPlayerAdminName(playerid));
SendClientMessage(playeridYOUR_COLORstring);
Result for Rank 2"You`re: Supporter" 
You can use this for every.
Examples: GetPlayerFactionName | GetPlayerVehicleName | GetPlayerAnyThingName ...

Also it dosnt have to be a player-based function. You can use this for everything.


How does it works?
PHP Code:
GetPlayerAdminName(playerid//Giving the function a name. Can be anything.
{
    new 
result[14]; //creating a string where the output will be stored in.
    
switch(PlayerInfo[playerid][pAdmin]) //switching the variable. In this case we switch the players adminrank.
    
{
        case 
0:{result="No Admin";}//in the case the player got the adminrank zero, we would call him "No Admin"
        
case 1:{result="Moderator";}//in the case 1, moderator.
        
case 2:{result="Supporter";}//etc...
        
case 3:{result="Administrator";}
    }
    return 
result;//after the switch is done, and the "result" is now filled with the players adminname, we return the result.

#Best Regards.
Reply
#2

I reckon that a constant array is shorter, still. But I don't know if it will actually be faster.
PHP Code:
// global
new const gAdminLevel[][] = {
    
"None"// 0
    
"Mod"// 1
    
"Support"// 2
    
"Admin" // 3
}; 
PHP Code:
//where ever
new string[64];
format(string,sizeof(string),"You`re: %s"gAdminLevel[PlayerInfo[playerid][pAdmin]]);
SendClientMessage(playeridYOUR_COLORstring); 
Still, it is good that repeated code is being denounced.
Reply
#3

Quote:
Originally Posted by Vince
View Post
I reckon that a constant array is shorter, still. But I don't know if it will actually be faster.
PHP Code:
// global
new const gAdminLevel[][] = {
    
"None"// 0
    
"Mod"// 1
    
"Support"// 2
    
"Admin" // 3
}; 
PHP Code:
//where ever
new string[64];
format(string,sizeof(string),"You`re: %s"gAdminLevel[PlayerInfo[playerid][pAdmin]]);
SendClientMessage(playeridYOUR_COLORstring); 
Still, it is good that repeated code is being denounced.
Yes, the constant array will be MUCH faster.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)