Looping through an enumerator array?
#1

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.
Reply
#2

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

or just

for(new p; p < 9; p++)
Reply
#3

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

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.
Reply
#5

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

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?
Reply
#7

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].
Reply
#8

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.
Reply
#9

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;
Reply
#10

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


Forum Jump:


Users browsing this thread: 1 Guest(s)