Question with format
#1

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.
Reply
#2

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.
Reply
#3

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.
Reply
#4

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


Forum Jump:


Users browsing this thread: 1 Guest(s)