Color help -
Mister0 - 18.07.2016
Hi, i made this sistem color who is saved in variable like this
PHP код:
new Color[16];//because I have 16 factions
then on a stock
stock PlayerColor(factionid)
{
format(Color[1],40,"0xFFFFFFC8");
format(Color[2],40,"0x0080C0C8");//si tot asa
return Color[factionid];//returnez culoarea de care am nevoie
}
then onplayerspawn
SetPlayerColor(playerid,PlayerColor(PlayerInfo[playerid][pMember]));
But when I spawn on server if I have member 1 or 2 my color is black, thats code is blue and white
what is the problem?
Re: Color help -
Misiur - 18.07.2016
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
Re: Color help -
Mister0 - 18.07.2016
ok ok i understand the problems but I left code because my color is black not that color i wanted, so here is the new code
is just a test code that hadded too much mistake
new Color[16] = { 0xFFFFFFC8, 0x0080C0C8};
stock PlayerColor(factionid)
{
return Color[factionid-1];//returnez culoarea de care am nevoie
}
onplayerspawn
SetPlayerColor(playerid,PlayerColor(PlayerInfo[playerid][pMember]));
Now is work thanks for this new Color[16] = { 0xFFFFFFC8, 0x0080C0C8, ... };
I didnt expect to work if I do this
Re: Color help -
Misiur - 18.07.2016
I used ... as an example, however it will work as it basically is equivalent to
pawn Код:
new Color[16] = { 0xFFFFFFC8, 0x0080C0C8, 0xFFFFFFC8, 0x0080C0C8, 0xFFFFFFC8, 0x0080C0C8, 0xFFFFFFC8, 0x0080C0C8, 0xFFFFFFC8, 0x0080C0C8, 0xFFFFFFC8, 0x0080C0C8, 0xFFFFFFC8, 0x0080C0C8, 0xFFFFFFC8, 0x0080C0C8 }
.