Transform this -
KinderClans - 28.10.2018
I have this:
pawn Код:
if(FactionInfo[playerid][factionmember] == 0)
To check if a player is in faction or not, but instead of writing this check at every command, i wanna make something like IsPlayerAdmin.
It should return true if is a faction member and false if isn't.
Something like if(IsPlayerInFaction).
How? Thanks.
Re: Transform this -
RogueDrifter - 28.10.2018
Doesn't make any sense, what's the point of creating another function if it's still going to be a check,
Like, creating this:
pawn Код:
bool:IsPlayerInFaction(playerid)
{
return (FactionInfo[playerid][factionmember]);
}
That's just creating a function to check a variable, same as checking the variable's value, no difference whatsoever, and if it's accepting 1's and 0's i advice adding the 'bool:' tag to identify that variable's type and using the char arrays to utilize less memory usage over the variable's capacity.
Re: Transform this -
TheToretto - 28.10.2018
PHP код:
IsFactionMember(playerid)
{
if(FactionInfo[playerid][factionmember] == 0)
return 1;
else
return 0;
}
CMD:test(playerid, params[])
{
if(IsFactionMember(playerid))
return SendClientMessage(playerid, -1, "You are a member of the faction.");
else
return SendClientMessage(playerid, -1, "You are not a member of the faction.");
}
Re: Transform this -
RogueDrifter - 28.10.2018
Quote:
Originally Posted by TheToretto
PHP код:
IsFactionMember(playerid)
{
if(FactionInfo[playerid][factionmember] == 0)
return 1;
else
return 0;
}
CMD:test(playerid, params[])
{
if(IsFactionMember(playerid))
return SendClientMessage(playerid, -1, "You are a member of the faction.");
else
return SendClientMessage(playerid, -1, "You are not a member of the faction.");
}
|
Just so you know, the if/else check to return either true or false is just the same as doing this:
pawn Код:
return (FactionInfo[playerid][factionmember]);
Re: Transform this -
BigETI - 29.10.2018
why not
pawn Код:
if (!FactionInfo[playerid][factionmember])
?
Re: Transform this -
GameOvr - 29.10.2018
Код:
IsPlayerInFaction(playerid)
{
return FactionInfo[playerid][factionmember];
}
Now you can use that like
if(IsPlayerInFaction(playerid) == 1)...
Re: Transform this -
IllidanS4 - 29.10.2018
pawn Код:
#define IsPlayerInFaction(%0) (FactionInfo[%0][factionmember] != 0)
Extra abstraction but the same efficiency.
Re: Transform this -
BigETI - 29.10.2018
pawn Код:
#define IsPlayerInFaction(%0) (FactionInfo[%0][factionmember] != 0)
#define IsPlayerReallyInFaction(%0) (IsPlayerInFaction(%0) != 0)
#define IsPlayerReallyReallyInFaction(%0) (IsPlayerReallyInFaction(%0) != 0)
#define IsPlayerReallyReallyReallyInFaction(%0) (IsPlayerReallyReallyInFaction(%0) != 0)
#define IsPlayerReallyReallyReallyReallyInFaction(%0) (IsPlayerReallyReallyReallyInFaction(%0) != 0)
Re: Transform this -
TheToretto - 29.10.2018
Quote:
Originally Posted by BigETI
pawn Code:
#define IsPlayerInFaction(%0) (FactionInfo[%0][factionmember] != 0) #define IsPlayerReallyInFaction(%0) (IsPlayerInFaction(%0) != 0) #define IsPlayerReallyReallyInFaction(%0) (IsPlayerReallyInFaction(%0) != 0) #define IsPlayerReallyReallyReallyInFaction(%0) (IsPlayerReallyReallyInFaction(%0) != 0) #define IsPlayerReallyReallyReallyReallyInFaction(%0) (IsPlayerReallyReallyReallyInFaction(%0) != 0)
|
lmaooo
Re: Transform this -
Y_Less - 29.10.2018
Quote:
Originally Posted by RogueDrifter
Doesn’t make any sense, what’s the point of creating another function if it’s still going to be a check,
Like, creating this:
Code:
bool:IsPlayerInFaction(playerid)
{
return (FactionInfo[playerid][factionmember]);
}
That’s just creating a function to check a variable, same as checking the variable’s value, no difference whatsoever, and if it’s accepting 1’s and 0’s i advice adding the ‘bool:’ tag to identify that variable’s type and using the char arrays to utilize less memory usage over the variable’s capacity.
|
There are very big advantages to using a function instead of a variable:
1) It makes your code more readable
if (IsPlayerX(i)) has way more meaning than
if (gPlayerData[i][E_PLAYER_X]).
2) It makes your code more maintainable. If you decide to change how a players X-ness is defined, you only need to change this in one function, instead of all over your code.
3) It isolates data. Pretty much all variables should be declared
static, which restricts them to a single file. You then provide access to the data through a fixed API in the form of your functions, not just letting anyone access it.
4) It allows you to control pre- and post-conditions. Maybe you want to do something like:
Code:
if (gPlayerData[i][E_PLAYER_X])
{
++gPlayerData[i][E_PLAYER_X_ACCESSES];
}
Either you add that in every time you check the variable, or you just put it in one place in your function:
Code:
bool:IsPlayerX(i)
{
if (gPlayerData[i][E_PLAYER_X])
{
++gPlayerData[i][E_PLAYER_X_ACCESSES];
return true;
}
return false;
}
And of course this can be modified or added at a later data again in just one place.
Well written code will almost never use variables directly except really internally in a module.
Re: Transform this -
RogueDrifter - 29.10.2018
Quote:
Originally Posted by ******
There are very big advantages to using a function instead of a variable:
1) It makes your code more readable if (IsPlayerX(i)) has way more meaning than if (gPlayerData[i][E_PLAYER_X]).
2) It makes your code more maintainable. If you decide to change how a players X-ness is defined, you only need to change this in one function, instead of all over your code.
3) It isolates data. Pretty much all variables should be declared static, which restricts them to a single file. You then provide access to the data through a fixed API in the form of your functions, not just letting anyone access it.
4) It allows you to control pre- and post-conditions. Maybe you want to do something like:
Код:
if (gPlayerData[i][E_PLAYER_X])
{
++gPlayerData[i][E_PLAYER_X_ACCESSES];
}
Either you add that in every time you check the variable, or you just put it in one place in your function:
Код:
bool:IsPlayerX(i)
{
if (gPlayerData[i][E_PLAYER_X])
{
++gPlayerData[i][E_PLAYER_X_ACCESSES];
return true;
}
return false;
}
And of course this can be modified or added at a later data again in just one place.
Well written code will almost never use variables directly except really internally in a module.
|
Good argument, agreed.
Re: Transform this -
TheToretto - 29.10.2018
Everyday you (I) learn something new...
Re: Transform this -
KinderClans - 29.10.2018
Just checked this topic and made me laugh about how many replies i got by a simple questions. Thank you guys!
Quote:
Originally Posted by BigETI
pawn Код:
#define IsPlayerInFaction(%0) (FactionInfo[%0][factionmember] != 0)
|
How i should use your define(s)?
In this way?
pawn Код:
if(IsPlayerInFaction(playerid)) bla bla
Right?
Re: Transform this -
Y_Less - 30.10.2018
Yes, they are like inlined functions.
Re: Transform this -
Kane - 30.10.2018
I\'m going to go on a limb here and say that your use of
FactionInfo is illogical.
Does the enumerator contain data that represents your player or faction system?
If it\'s the latter,
factionmember should belong to the enumerator that represents your player data.