SA-MP Forums

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

Reply
 
Thread Tools Display Modes
Old 08/02/2016, 10:57 PM   #1
GangstaSunny
Big Clucker
 
Join Date: Nov 2014
Posts: 153
Reputation: 45
Default "Function" your variable-names - Make your life easier!

(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.
GangstaSunny is offline   Reply With Quote
Old 08/02/2016, 11:28 PM   #2
Vince
Spam Machine
 
Vince's Avatar
 
Join Date: Sep 2007
Location: Belgium
Posts: 10,964
Reputation: 2649
Default Re: "Function" your variable-names - Make your life easier!

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.
__________________
Vince is offline   Reply With Quote
Old 09/02/2016, 05:41 PM   #3
Crayder
High-roller
 
Crayder's Avatar
 
Join Date: Sep 2013
Location: Flames of Hell
Posts: 3,825
Reputation: 603
Default Re: "Function" your variable-names - Make your life easier!

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.
__________________
Those who deserve reputation, do not need to beg for it.
Also, don't expect the help you need when offering reputation, you'll just be attracting Rep Hunters.
Join SA-MP Discord!
Crayder 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
Expecting "]" found ";" Doesn't Make Sense Nathan_Taylor Scripting Help 5 03/05/2014 12:55 AM
Deciding how to store "mission info" driftg0d Scripting Help 1 09/01/2014 03:46 AM
5 Dialog errors - Input line too long, undefined symbol "t","is" and "only" Deal-or-die Scripting Help 6 01/05/2012 01:08 PM
"Slight homing", make a missile "Slightly!" home a vehicle. Donya Help Archive 4 11/06/2011 10:20 PM


All times are GMT. The time now is 01:58 AM.


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