SA-MP Forums Archive
Question with format - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Question with format (/showthread.php?tid=597708)



Question with format - K9IsGodly - 03.01.2016

PHP код:
new IDrankstring[128]; 
PHP код:
if(rank == '1')
        {
            
PlayerInfo[playerid][nlevel] = 1;
            
format(stringsizeof(string), "You have changed %s's newb rank to helper"ID);
            
SendClientMessage(playerid, -1string);
            
format(stringsizeof(string), "Your newb rank has been set to helper by Admin %s"playerid);
            
SendClientMessage(playerid, -1string);
        }
        else if(
rank == '2')
        {
            
PlayerInfo[playerid][nlevel] = 2;
            
format(stringsizeof(string), "You have changed %s's newb rank to Advisor"ID);
            
SendClientMessage(playerid, -1string);
            
format(stringsizeof(string), "Your newb rank has been set to Advisor by Admin %s"playerid);
            
SendClientMessage(playerid, -1string);
        }
        else if(
rank == '3')
        {
            
PlayerInfo[playerid][nlevel] = 3;
            
format(stringsizeof(string), "You have changed %s's newb rank to Lead Advisor"ID);
            
SendClientMessage(playerid, -1string);
            
format(stringsizeof(string), "Your newb rank has been set to Lead Advisor by Admin %s"playerid);
            
SendClientMessage(playerid, -1string);
        } 
I use format(string) for every instance, instead of creating a string2 and so on. Will this work? And, if so, is it bad practice to do so? I was pretty sure it doesn't matter that's why I want to ask.


Re: Question with format - saffierr - 03.01.2016

It doesn't matter at all. Everyone has his own style of scripting, I do it like this way either.
If this is clear and the most easy way for you, I'd say keep on going like this.


Re: Question with format - AndySedeyn - 03.01.2016

It is not a bad practice. However, there are a few things wrong with the code you provided. For one, you're checking an integer variable, not a string.

PHP код:
if(rank == 1
Secondly, these kind of if-statements and especially when there are else-if's involved can be replaced by a switch statement:

PHP код:
switch(rank) {
    case 
1: {
        
// rank == 1
    
}
    case 
2: {
        
// rank == 2
    
}
    case 
3: {
        
// rank == 3
    
}
    default: {
        
// rank is nor 1, nor 2 and nor 3 - Not of any use in your case
    
}

Thirdly, you can assign the player's nLevel variable outside all these checks:
PHP код:
switch(rank) {
    case 
1: {
        
// rank == 1
    
}
    case 
2: {
        
// rank == 2
    
}
    case 
3: {
        
// rank == 3
    
}
    default: {
        
// rank is nor 1, nor 2 and nor 3 - Not of any use in your case
    
}
}
PlayerInfo[playerid][nLevel] = rank;
// Alternatively:
if(/* check */) { 
    
// Code
}
else if(
/* check */) { 
    
// Code
}
else if(
/* check */) { 
    
// Code
}
PlayerInfo[playerid][nLevel] = rank
Also, you're formatting names, but you never get the player's name. You're formatting string specifiers with an integer. That won't work.


Re: Question with format - Godey - 04.01.2016

There are different types of scripting, Nothing is a bad practise, depends on the one you choose.