Color help
#2

Ok, step by step. SetPlayerColor accepts an integer (a number) in hexadecimal format, i.e. 0xFFFFFFFF, NOT a string "0xFFFFFFFF".

pawn Код:
stock PlayerColor(factionid)
{
Color[1] = 0xFFFFFFC8;
Color[2] = 0x0080C0C8;//si tot asa
return Color[factionid];//returnez culoarea de care am nevoie
}
Step 2: You are wasting 1 slot, as arrays start from 0:
pawn Код:
stock PlayerColor(factionid)
{
Color[0] = 0xFFFFFFC8;
Color[1] = 0x0080C0C8;//si tot asa
return Color[factionid - 1];//returnez culoarea de care am nevoie
}
Step 3: You are setting faction colors every time you call PlayerColor. That's bad.
pawn Код:
new Color[16] = { 0xFFFFFFC8, 0x0080C0C8, ... };

stock PlayerColor(factionid)
{
return Color[factionid - 1];//returnez culoarea de care am nevoie
}
Step 4: Make sure factionid returns a valid value:
pawn Код:
new Color[16] = { 0xFFFFFFC8, 0x0080C0C8, ... };

#define IsAValidFaction(%0) (0 <= %0 <= 16)

stock PlayerColor(factionid)
{
if (!IsAValidFaction(factionid)) return 0x000000FF;
return Color[factionid - 1];//returnez culoarea de care am nevoie
}
Step 5: But what if one day you want 1 more faction?
pawn Код:
#define MAX_FACTIONS 16
new Color[MAX_FACTIONS] = { 0xFFFFFFC8, 0x0080C0C8, ... };

#define IsAValidFaction(%0) (0 <= %0 <= MAX_FACTIONS)

stock PlayerColor(factionid)
{
if (!IsAValidFaction(factionid)) return 0x000000FF;
return Color[factionid - 1];//returnez culoarea de care am nevoie
}
Step 6: Now you have shitton of magic numbers, i.e if you want to check if player is a policeman you do PlayerInfo[pid][pFaction] == 3. That's bullshit! Let's fix that as well:

pawn Код:
enum E_FACTION {
    E_FACTION_NONE,
    E_FACTION_ARMY,
    E_FACTION_GOV,
    E_FACTION_POLICE,
    //etc.
}
new Color[_:E_FACTION] = { 0xFFFFFFC8, 0x0080C0C8, ... };

#define IsAValidFaction(%0) (0 <= %0 <= _:E_FACTION)

stock PlayerColor(factionid)
{
if (!IsAValidFaction(factionid)) return 0x000000FF;
return Color[factionid];//returnez culoarea de care am nevoie
}
There. Much cleaner
Reply


Messages In This Thread
Color help - by Mister0 - 18.07.2016, 20:26
Re: Color help - by Misiur - 18.07.2016, 20:36
Re: Color help - by Mister0 - 18.07.2016, 20:48
Re: Color help - by Misiur - 18.07.2016, 20:58

Forum Jump:


Users browsing this thread: 2 Guest(s)