Transform this
#1

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

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

PHP код:
IsFactionMember(playerid)
{
    if(
FactionInfo[playerid][factionmember] == 0)
        return 
1;
    else
        return 
0;
}
CMD:test(playeridparams[])
{
    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.");

Reply
#4

Quote:
Originally Posted by TheToretto
Посмотреть сообщение
PHP код:
IsFactionMember(playerid)
{
    if(
FactionInfo[playerid][factionmember] == 0)
        return 
1;
    else
        return 
0;
}
CMD:test(playeridparams[])
{
    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]);
Reply
#5

why not
pawn Код:
if (!FactionInfo[playerid][factionmember])
?
Reply
#6

Код:
IsPlayerInFaction(playerid)
{
     return FactionInfo[playerid][factionmember];
}


Now you can use that like
if(IsPlayerInFaction(playerid) == 1)...
Reply
#7

pawn Код:
#define IsPlayerInFaction(%0) (FactionInfo[%0][factionmember] != 0)
Extra abstraction but the same efficiency.
Reply
#8

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)
Reply
#9

Quote:
Originally Posted by BigETI
View Post
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
Reply
#10

Quote:
Originally Posted by RogueDrifter
View Post
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.
Reply
#11

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

Everyday you (I) learn something new...
Reply
#13

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?
Reply
#14

Yes, they are like inlined functions.
Reply
#15

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


Forum Jump:


Users browsing this thread: 1 Guest(s)