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.