Loop through enum items
#1

Hey!

Is there any way to loop through an array that has items of an enum?

pawn Код:
enum PlayerInfo { deaths, fstyle, kicks, kills, money, muted, mutes, rank, respect, score, skin };

new g_PlayerInfo[MAX_PLAYERS][PlayerInfo];
I ask it because using

pawn Код:
g_PlayerInfo[playerid][deaths] = 0;
g_PlayerInfo[playerid][fstyle] = 0;
g_PlayerInfo[playerid][kicks] = 0;
...
when a player connects is more complicated if you have much more enum items.


Thanks
Nonameman
Reply
#2

Yes just do the loop and replace the field with your loop.
Leave the player id alone

g_PlayerInfo[playerid][i] = 0
Reply
#3

No, I know how to loop through a normal array, but it generates tag mismatch error, it can't be indexed with integer values.
Reply
#4

pawn Код:
for(new i; PlayerInfo:i < PlayerInfo; i++)
{
     g_PlayerInfo[playerid][PlayerInfo:i] = 0;
}
something like this should do it..

edit: not tested atm.
Reply
#5

Thank you so much, this solved my problem.
Can I ask you to explain me who is it works? It's like an iterator?
Reply
#6

the enum name is also the tag of the index variable.
Reply
#7

Thank you.
Reply
#8

https://sampforum.blast.hk/showthread.php?tid=318307


Actually what i posted does work via the link above
Reply
#9

hint: you could also use the _ key for this:
pawn Код:
for(new i; _:i < PlayerInfo; i++)
{
     g_PlayerInfo[playerid][PlayerInfo:i] = 0;
}
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)