PDA

View Full Version : Looping through an enumerator array?


CalvinC
06/02/2015, 09:43 PM
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:
enum test
{
something[9]
}
new Testing[MAX_PLAYERS][test];

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.

Jefff
06/02/2015, 10:19 PM
Try
for(new p; p < sizeof(Testing[]); p++)

or just

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

CalvinC
06/02/2015, 10:53 PM
Oh yeah i should have thought about just doing for(new p; p < 9; p++) Didn't think about that, thanks Jefff. :P

Schneider
06/02/2015, 10:59 PM
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.

Knappen
06/02/2015, 11:03 PM
for(new Stat = 0; pInfo:Stat < pInfo; Stat++)
{
PlayerInfo[playerid][pInfo:Stat] = 0;
}

This is how I usually do it.

CalvinC
06/02/2015, 11:19 PM
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.
error 039: constant symbol has no size
error 029: invalid expression, assumed zero
error 029: invalid expression, assumed zero


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?

Knappen
06/02/2015, 11:43 PM
Say I have an enumerator like this:


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.

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.


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].

CalvinC
06/02/2015, 11:57 PM
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?
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.

Knappen
07/02/2015, 12:15 AM
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


for(new Something = 0; Something < 9; Something++)
{
SomeEnum[playerid][Something] = 0;
}


If it's a string, you can do


SomeEnum[playerid][SomeConstant] = EOS;

Jefff
07/02/2015, 01:18 AM
for(new p=0; p < _:test; p++)
Testing[playerid][something][p] = 0;

_:test is a size of enum, there is 9 elements

CalvinC
07/02/2015, 08:49 AM
Thanks Jefff, it doesn't give errors, im gonna test if it works later.