Player data access style
#1

Hi guys! I'm writing something and wanted to ask you for opinion. Almost all servers for some reason (maybe Godfather legacy "style" "guide") keep player data like this:
pawn Код:
enum pInfo {
    // (...)
    Float:pHealth,
    pInt,
    pVW
    // (...)
}

new PlayerInfo[MAX_PLAYERS][pInfo];
pInfo grows to enormous sizes and it's hard to navigate. Additionally you have to remember to put "p" in front of the property you want when accessing PlayerInfo.

Now this is my idea:
pawn Код:
#define Player.%0[%1][%2] Player%0[%1][E_PLAYER_%0_%2]

enum E_PLAYER_META {
    E_PLAYER_Meta_username[MAX_PLAYER_NAME + 1],
    E_PLAYER_Meta_salt[SOME_SALT_SIZE_CONSTANT],
    E_PLAYER_Meta_password[256 + 1]
}

new PlayerMeta[MAX_PLAYERS][E_PLAYER_META];

enum E_PLAYER_STATUS {
    Float:E_PLAYER_Status_health,
    Float:E_PLAYER_Status_armour,
    E_PLAYER_Status_interior,
    E_PLAYER_Status_vw
}

new PlayerStatus[MAX_PLAYERS][E_PLAYER_STATUS];

// Accessing:
strcpy(Player.Meta[playerid][username], "New_Username");

if (Player.Status[playerid][health] < 50.0) // blah blah
First of all, yes, the "UPPER_Title_lower" is ugly. Long enum field names are on purpose to avoid collisions. But as you can see with simple macro it's even easier than remembering "pHealth" and friends.
One problem which arises is sizeof second dimension (present when doing anything with strings). But I require Zeex's compiler, so that's not an issue.
Second problem would be rare cases where you want to access enum field by offset, say for copying with memcpy or clearing with memset. Then Player.Meta[playerid][0] would turn into invalid PlayerMeta[playerid][E_PLAYER_Meta_0]. Ideas are welcome, probably something with detecting character with macros.

What are your thoughts guys? Do you see any additional problems?
Reply


Messages In This Thread
Player data access style - by Misiur - 17.08.2017, 13:23
Re: Player data access style - by Paulice - 17.08.2017, 16:12
Re: Player data access style - by Vince - 17.08.2017, 16:37
Re: Player data access style - by Misiur - 17.08.2017, 19:48
Re: Player data access style - by Paulice - 17.08.2017, 21:43
Re: Player data access style - by Misiur - 17.08.2017, 21:53
Respuesta: Re: Player data access style - by Marricio - 20.08.2017, 14:29
Re: Player data access style - by TheCman - 20.08.2017, 16:03

Forum Jump:


Users browsing this thread: 1 Guest(s)