SA-MP Forums Archive
Looping through an enumerator array? - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Looping through an enumerator array? (/showthread.php?tid=562092)



Looping through an enumerator array? - CalvinC - 06.02.2015

How do you loop through an array in a enumerator? (I think i've heard that it's called something else, but can't remember what)
Etc. something like this:
pawn Код:
enum test
{
    something[9]
}
new Testing[MAX_PLAYERS][test];
pawn Код:
CMD:test(playerid, params)
{
    for(new p; p < sizeof(Testing[playerid][something]); p++)
    {
        Testing[playerid][something][p] = 0;
    }
}
Although it doesn't work, i've tried many different methods, but i can't get it working, couldn't find anything about this when searching either.


Re: Looping through an enumerator array? - Jefff - 06.02.2015

Try
for(new p; p < sizeof(Testing[]); p++)

or just

for(new p; p < 9; p++)


Re: Looping through an enumerator array? - CalvinC - 06.02.2015

Oh yeah i should have thought about just doing
pawn Код:
for(new p; p < 9; p++)
Didn't think about that, thanks Jefff. :P


Re: Looping through an enumerator array? - Schneider - 06.02.2015

I think it's better to use Jefff's first method. If you ever have to change the size of the array, you won't have to through your script to change all the values manually.


Re: Looping through an enumerator array? - Knappen - 06.02.2015

pawn Код:
for(new Stat = 0; pInfo:Stat < pInfo; Stat++)
{
    PlayerInfo[playerid][pInfo:Stat] = 0;
}
This is how I usually do it.


Re: Looping through an enumerator array? - CalvinC - 06.02.2015

Quote:
Originally Posted by Schneider
Посмотреть сообщение
I think it's better to use Jefff's first method. If you ever have to change the size of the array, you won't have to through your script to change all the values manually.
Jefff's first method didn't work, returned errors aswell.
Quote:

error 039: constant symbol has no size
error 029: invalid expression, assumed zero
error 029: invalid expression, assumed zero

Quote:
Originally Posted by Knappen
Посмотреть сообщение
pawn Код:
for(new Stat = 0; pInfo:Stat < pInfo; Stat++)
{
    PlayerInfo[playerid][pInfo:Stat] = 0;
}
This is how I usually do it.
Since you're using completey different stuff, can you explain what it does, or show it with my coding examples?


Re: Looping through an enumerator array? - Knappen - 06.02.2015

Say I have an enumerator like this:

pawn Код:
enum pInfo
{
    Admin,          // 0
    Level,            // 1
    Float:Health,  // 2
    Float:Armor   // 3
};
new PlayerInfo[MAX_PLAYERS][pInfo];
In a normal enum, it will make an index or reference to the constants. Like noted above.

If I want to access the variables without writing the "names" of them I do like this.
pawn Код:
SetPlayerHealth(playerid, PlayerInfo[playerid][2]); // 2 being Health in my enumerator.

OR

if(!PlayerInfo[playerid][0] >= 1} // 0 being admin in my enumerator
{
    // Do something
}
For example on OnPlayerConnect, I want to set all these to 0 to make sure the connecting player doesn't get anyone else's stats.
For that I would do this.

pawn Код:
for(new Stat = 0; pInfo:Stat < pInfo; Stat++)
{
    PlayerInfo[playerid][pInfo:Stat] = 0;
}
The "Stat" variable is will be used as the index in the enum. It basically loops through every item and sets it to 0.

Sorry for not being able to explain that in a better way.

Edit:
pInfo:Stat < pInfo

The value of pInfo will be the value of the last constant created in the enumerator if I'm not mistaken, in this case 3. pInfo:Stat is just a different way of writing PlayerInfo[playerid][Stat].


Re: Looping through an enumerator array? - CalvinC - 06.02.2015

When i looked at it, it just looked like it would loop through the whole "enum pInfo", and not just a given array, but i guess im wrong.
So then in my case i would do this?
pawn Код:
for(new something = 0; test:something; < test; something++)
But i don't think that'll work, first of all, it causes alot of errors.
Second of all, i need "something" to have a value. Etc Testing[playerid][something][2] = 4;
And if so, i think the loop would set it back to 0 and raise it to (in my case) 9, since that's how big "something"'s array is, thereby ruining the value i need.


Re: Looping through an enumerator array? - Knappen - 07.02.2015

I'm not quite sure what you are trying to achieve.
Are you trying to loop through an array or the enumerator itself?

If you want to loop through an array with 9 cells, just do

pawn Код:
for(new Something = 0; Something < 9; Something++)
{
    SomeEnum[playerid][Something] = 0;
}
If it's a string, you can do

pawn Код:
SomeEnum[playerid][SomeConstant] = EOS;



Re: Looping through an enumerator array? - Jefff - 07.02.2015

pawn Код:
for(new p=0; p < _:test; p++)
    Testing[playerid][something][p] = 0;
_:test is a size of enum, there is 9 elements