Jump to content
Ninjaisfast

from from to looping help

Recommended Posts

I'm trying to get my head around how to loop a script with dynamic steps. I want to have the script do 1 thing on the first loop, another on the next 4 loops, then another on the last 5. I've tried looking at the examples in the 'for' wiki but can't quite understand it without some laymans terms and explaining.

The wiki shows:

_xy = [1,2,3,4,5,6,7,8,9,10];
for [{_i=1},{_i<=(count _xy - 1)},{_i=_i+1}] do {
	if ( _xy select _i == 3 ) then {
		_xy deleteAt _i;
		_i = _i - 1;	
	};
};

I'm pretty sure it would look similar to this with steps for the other loops, but I don't understand how the second line actually works.

Share this post


Link to post
Share on other sites

Also is this something that would be easier to use the while command instead?

Share this post


Link to post
Share on other sites

it would be much easier to help if you would explain the whole thing you want to do.

  • Like 2

Share this post


Link to post
Share on other sites
2 hours ago, Ninjaisfast said:

Also is this something that would be easier to use the while command instead?

Depends on what you want, as @sarogahtyp stated explain what exactly you're trying to do.

A loop is a loop is a loop. Which kind of loop you're using depends on what you want the loop to do.

 

forEach loops through an array and gives you _forEachIndex, could be an alternative to the from to loop.

If you don't need an index you can use while or count.

 

Cheers

  • Like 1

Share this post


Link to post
Share on other sites

Whatever you do, do not alter array you are iterating through by removing elements!

  • Like 1

Share this post


Link to post
Share on other sites
21 hours ago, Ninjaisfast said:

I'm trying to get my head around how to loop a script with dynamic steps. I want to have the script do 1 thing on the first loop, another on the next 4 loops, then another on the last 5. I've tried looking at the examples in the 'for' wiki but can't quite understand it without some laymans terms and explaining.

The wiki shows:


_xy = [1,2,3,4,5,6,7,8,9,10];
for [{_i=1},{_i<=(count _xy - 1)},{_i=_i+1}] do {
	if ( _xy select _i == 3 ) then {
		_xy deleteAt _i;
		_i = _i - 1;	
	};
};

I'm pretty sure it would look similar to this with steps for the other loops, but I don't understand how the second line actually works.

 

Loops aren't magical. They only repeat a set of instructions as long as the correct conditions apply.

The for loop is built to iterate over a pre-determined set. In layman terms: Use for when you know how many loops you want. Use while when you don't.

All for does is update the variable and execute the code you provided with the new result.

It's better understood in this construct (which happens to be faster): 

for "_x" from 10 to 20 do {..code..}

where _x is the variable we iterate (the name is arbitrary, keep it short and sweet though), 10 is the starting value of _x and 20 is the value _x will hold on the last iteration.

 

If you want your looped-code to react to changes in the variable you have to provide conditions for it to do so e.g.:

Spoiler

 


//*sarcasm* The almighty if-wall
for "_a" from 0 to 10 do {
	if(_a == 0) then {
		//_a == 0
	};
	if(_a == 1) then {
		//_a == 1
	};
	if(_a == 2) then {
		//_a == 2
	};
	//... etc
};

//You could also use a switch
for "_a" from 0 to 10 do {
	switch (_a) do {
		case 0: { /* _a == 0 */ };
		case 1: { /* _a == 1 */ };
		case 2: { /* _a == 2 */ };
		default { /* _a not 0,1 or 2 */ };
	};
};

 

 

 

However that's a bit of an inefficient way of going about it. You want to "do 1 thing on the first loop, another on the next 4 loops, then another on the last 5.". So the first thing happens once, the next thing happens 4 times but never again and the last thing happens 5 times and then we're done. There is no need to put it all in one loop because once the first set of instructions has been executed they are no longer needed. So... second revision: 

Spoiler

//*****************************************
//Here I'm assuming you do care about the variable you are iterating over.
//*****************************************
private _a = 0;

//_a == 0, do the step 1 here

for "_i" from 0 to 3 do {
	_a = _a + 1;
	//_a will be 1,2,3,4 here, do step 2 here	
};

for "_i" from 0 to 4 do {
	_a = _a + 1;
	//_a will be 5,6,7,8,9 here, do step 3 here
};

//*****************************************
//Here I'm assuming you dont care about the variable, you heartless monster you.
//*****************************************

//do step 1 here

for "_i" from 0 to 3 do {
	//step 2 here
};

for "_i" from 0 to 4 do {
	//step 3 here
};

 

 

 

25 minutes ago, killzone_kid said:

Whatever you do, do not alter array you are iterating through by removing elements!

I agree, great rule of thumb, but that code Ninjaisfast put up is taken straight from Nickorr's comment on the wiki which is an example on doing exactly that. ^^

  • Like 1

Share this post


Link to post
Share on other sites
18 minutes ago, mrcurry said:

So... second revision: 

  Reveal hidden contents


//*****************************************
//Here I'm assuming you do care about the variable you are iterating over.
//*****************************************
private _a = 0;

//_a == 0, do the step 1 here

for "_i" from 0 to 3 do {
	_a = _a + 1;
	//_a will be 1,2,3,4 here, do step 2 here	
};

for "_i" from 0 to 4 do {
	_a = _a + 1;
	//_a will be 5,6,7,8,9 here, do step 3 here
};

//*****************************************
//Here I'm assuming you dont care about the variable, you heartless monster you.
//*****************************************

//do step 1 here

for "_i" from 0 to 3 do {
	//step 2 here
};

for "_i" from 0 to 4 do {
	//step 3 here
};

 

 


I think this will work for what I need, thanks. I've been having some trouble with (I think) incorrectly redifining a variable. I'm going to give it a new try using your template and update here if I have trouble. There's something with the use of 'private' that I'm getting confused with. Basically in the first step, I want my local variable to be the first item in an array, then afterward that variable to be any OTHER random item in that array. I'm confusing myself and probably anyone reading this, I'll give things a try and be back with an update.

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now

×