Convert "for" to "foreach"
#1

Hi all!

I have an array with the user IDs, with a function a must toggle all the players in this array to "controllable" with "TogglePlayerControllable"

I'm not able to convert the following for loop to a foreach loop using the standalone foreach or the y_less/iterate.

RunData[sRun][RunnersCount] is an integer
RunData[sRun][Runners] is an array

Code:
for(new sP = 1; sP <= RunData[sRun][RunnersCount]; sP++)
{
	TogglePlayerControllable(RunData[sRun][Runners][sP], 1);
}
Can someone help me?

Regards
Reply
#2

What is RunData[sRun][Runners][sP] ?


You can make a foreach loop with a player variable. For example, if Runners are those players who's participate to event make a variable like :
HTML Code:
new OnEvent[MAX_PLAYERS];
When the players enter in event, put
HTML Code:
OnEvent[playerid] = 1;
then, make your loop:

HTML Code:
foreach(Player, i)
{
        if(OnEvent[i] == 1)
       {
	TogglePlayerControllable(i, 1);
       }
}
Reply
#3

Quote:
Originally Posted by Nin9r
View Post
What is RunData[sRun][Runners][sP] ?


You can make a foreach loop with a player variable. For example, if Runners are those players who's participate to event make a variable like :
HTML Code:
new OnEvent[MAX_PLAYERS];
When the players enter in event, put
HTML Code:
OnEvent[playerid] = 1;
then, make your loop:

HTML Code:
foreach(Player, i)
{
        if(OnEvent[i] == 1)
       {
	TogglePlayerControllable(i, 1);
       }
}
RunData[sRun][Runners][sP] is the index of the array, sP is defined from the for loop.

I can't just check if the player is "OnEvent" because i have more than one match running so i must know in which match change the controllable status.
The match when is created is in "waiting for players" and only when the minimun players quantity is reached i execute the loop.
Is like a lobby system

_Edit

but, i assign an id to each event and i already store for each player in which event is, so i can

HTML Code:
foreach(Player, i)
{
        if(CharData[i][WarID] == MatchID) //the MatchID is sent throught the funcion vars
       {
	TogglePlayerControllable(i, 1);
       }
}
Reply
#4

So what? Use a count.

new count;

count++.

if(count > value)
{
execute loop
}
Reply
#5

Quote:
Originally Posted by Nin9r
View Post
So what? Use a count.

new count;

count++.

if(count > value)
{
execute loop
}
Sorry but i don't understand why..
Reply
#6

If you really want to turn that into a foreach loop you have to change your code structure, potentially some of those arrays (as foreach will kinda do the work of your array if it just keeps runner ids, making it useless)

A) You need to create a custom iterator and create it globally I.E: new Iterator: Runners<MAX_PLAYERS>;
B) You need to add the player ID to iterator using Iter_Add(Runners, id);
C) On approperiate parts (like /leaverun and disconnections) you need to remove them from the iterator with Iter_Remove
D) simply loop thru players running with foreach( new i : Runners)
now go and create that, and I would love for you rep hunters not to hand him what I exactly explained for him he can't rep, get the h out of this thread and let him learn some pawn.

(Hint: If you have more than 1 race runners you can create multi-dimensional Iterators which can be accessed like Runners[0]..Runners[1000], read more on this in foreach's thread)
Reply
#7

Quote:
Originally Posted by PrO.GameR
View Post
If you really want to turn that into a foreach loop you have to change your code structure, potentially some of those arrays (as foreach will kinda do the work of your array if it just keeps runner ids, making it useless)

A) You need to create a custom iterator and create it globally I.E: new Iterator: Runners<MAX_PLAYERS>;
B) You need to add the player ID to iterator using Iter_Add(Runners, id);
C) On approperiate parts (like /leaverun and disconnections) you need to remove them from the iterator with Iter_Remove
D) simply loop thru players running with foreach( new i : Runners)
now go and create that, and I would love for you rep hunters not to hand him what I exactly explained for him he can't rep, get the h out of this thread and let him learn some pawn.

(Hint: If you have more than 1 race runners you can create multi-dimensional Iterators which can be accessed like Runners[0]..Runners[1000], read more on this in foreach's thread)
Thanks for the great reply.
I sow the thread this morning but i had no time to read it with attention.
Tomorrow i have 4 hours to spend at the airport and maybe i can read it well..
Let you know!
Reply
#8

Late asf, -removed-
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)