Question with format -
K9IsGodly - 03.01.2016
PHP код:
new ID, rank, string[128];
PHP код:
if(rank == '1')
{
PlayerInfo[playerid][nlevel] = 1;
format(string, sizeof(string), "You have changed %s's newb rank to helper", ID);
SendClientMessage(playerid, -1, string);
format(string, sizeof(string), "Your newb rank has been set to helper by Admin %s", playerid);
SendClientMessage(playerid, -1, string);
}
else if(rank == '2')
{
PlayerInfo[playerid][nlevel] = 2;
format(string, sizeof(string), "You have changed %s's newb rank to Advisor", ID);
SendClientMessage(playerid, -1, string);
format(string, sizeof(string), "Your newb rank has been set to Advisor by Admin %s", playerid);
SendClientMessage(playerid, -1, string);
}
else if(rank == '3')
{
PlayerInfo[playerid][nlevel] = 3;
format(string, sizeof(string), "You have changed %s's newb rank to Lead Advisor", ID);
SendClientMessage(playerid, -1, string);
format(string, sizeof(string), "Your newb rank has been set to Lead Advisor by Admin %s", playerid);
SendClientMessage(playerid, -1, string);
}
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.
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.