Extract String from enum
#1

How would you go about extracting strings from an enum?

For example, we have a system where only certain users have access. Each time we add a command, we have to use strcmp(pName, ...) to check if it is an authorized person.

I have seen it before, but I can't remember how it was done. I would like to create an enum with the list of authorized people, and have it check this list each time instead of using strcmp to check names.

Thanks in advance
Reply
#2

new MyAccessList[MAX_PLAYERS][32];

stock HasAccess(playerid){
new xS[32];
for(new x; a < MAX_PLAYERS; x++){
GetPlayerName(playerid,xS,32);
if(strcmp...){
return 1;// and owned.. wqe still use strcmp.
}
}
return 0;
}

well this is a basic example, it can be expanded with many more features,.
Reply
#3

CAn't you do like:

PlayerEnumInfo[playerid][Authorized]

So:

if(PlayerEnumInfo[playerid][Authorized] == 1)
{

//Is authorized

}
else
{

//Not authorized.
}
Reply
#4

Should it be x< MAX_PLAYERS, or is the "a" intended?

And there is not any other information stored per player that would need an enum, and I don't necessarily want to use this per player.

so for example

pawn Код:
enum Authorized
{
     Player_a,
     Player_b,
     Player_c
}
and then see if the player's name is on that list for a command.
Reply
#5

You do not really need an enum for that, a string array would do the job too

pawn Код:
new Names[][MAX_PLAYER_NAME] =
{
    name_1,
    name_2,
    ...
};

//To check if a name is in the list:
for(new i = 0; i < sizeof(Names); i ++)
{
    if(!strcmp(playername, Names[i], false))
    {
        //It is on the list
        //...your code...
        break;
    }
}
Reply
#6

Thanks, thats what I needed!
Reply
#7

Ok, it compiles fine now, but things aren't name-locked. This is what I have:

pawn Код:
forward IsAuthorized(playerid);

new Names[][MAX_PLAYER_NAME] =
{
    "John_Doe",
        // More names
};

public IsAuthorized(playerid)
{
     for(new i = 0; i < sizeof(Names); i ++)
    {
    new playername[MAX_PLAYER_NAME];
    GetPlayerName(playerid, playername, sizeof(playername));
    if(!strcmp(playername, Names[i], false)) {}
    }
    return 1;
}
Everyone can use the commands if I do if(IsAuthorized(playerid)) as a conditional. Can anyone spot an error?
Thanks again
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)