Jump to content
notproplayer3

while loop inside forEach

Recommended Posts

This script was meant to make all the units of a squad salute

so the problem is that the _x variable in the while loop seems to be working for only one unit of the group

myUnits = units _mysquad
{_x switchMove"AmovPercMstpSnonWnonDnon_SaluteIn";
while{s2} do {
_x PlayMoveNow"AmovPercMstpSnonWnonDnon_Salute";
};
_x switchMove "AmovPercMstpSnonWnonDnon_SaluteOut";
} forEach myUnits

s2 is a boolean that changes to false after a certain amount of time in another script

s2=true;
sleep 6;
s2=false;

and _mysquad is simply the variable name of a squad composition

like I said the _x variable outside the while loop works fine but when it is inside that loop it works for only 1 unit for some reason (my squad has 6 units)

I am aware that this portion of the script could totally be fine so tell me if it is, I say that because I reduced my code in this thread to the only portion I thought was incorrect 

Share this post


Link to post
Share on other sites
1 hour ago, notproplayer3 said:

This script was meant to make all the units of a squad salute

so the problem is that the _x variable in the while loop seems to be working for only one unit of the group


myUnits = units _mysquad
{_x switchMove"AmovPercMstpSnonWnonDnon_SaluteIn";
while{s2} do {
_x PlayMoveNow"AmovPercMstpSnonWnonDnon_Salute";
};
_x switchMove "AmovPercMstpSnonWnonDnon_SaluteOut";
} forEach myUnits

s2 is a boolean that changes to false after a certain amount of time in another script


s2=true;
sleep 6;
s2=false;

and _mysquad is simply the variable name of a squad composition

like I said the _x variable outside the while loop works fine but when it is inside that loop it works for only 1 unit for some reason (my squad has 6 units)

I am aware that this portion of the script could totally be fine so tell me if it is, I say that because I reduced my code in this thread to the only portion I thought was incorrect 

 

You can always spawn something from inside the forEach, like this:

myUnits = units _mysquad;

{
_salute = _x spawn {
sleep random 1;//so they don't salute all at the same time
_this switchMove"AmovPercMstpSnonWnonDnon_SaluteIn";
waitUntil {s2};
_this PlayMoveNow"AmovPercMstpSnonWnonDnon_Salute";
waitUntil {!s2};
sleep random 1;
_this switchMove "AmovPercMstpSnonWnonDnon_SaluteOut";

};

} forEach myUnits

 

Though it's better to put all that stuff in a separate function to keep it somewhat readable:

 

//init.sqf or wherever you seem fit:

TAG_fnc_salute = {

	params ["_unit"];
	sleep random 1;//so they don't salute all at the same time
	_unit switchMove"AmovPercMstpSnonWnonDnon_SaluteIn";
	waitUntil {s2};
	_unit PlayMoveNow"AmovPercMstpSnonWnonDnon_Salute";
	waitUntil {!s2};
	sleep random 1;
	_unit switchMove "AmovPercMstpSnonWnonDnon_SaluteOut";
};

myUnits = units _mysquad;

{
_x spawn TAG_fnc_salute;
} forEach myUnits;

This way it's all happening in a centralized function.

Also changed the while loop to a waituntil since it makes more sense, a while loop would constantly spam the animation.

 

Cheers

  • Like 2
  • Thanks 1

Share this post


Link to post
Share on other sites

oh, I see, thanks as always for your help, I don't know what I'll be doing without you on the BI community

  • Like 1

Share this post


Link to post
Share on other sites

Is waitUntil even needed? Why not use playMove (I think it is) instead of switchMove then animations will queue.

Quote

The difference between playMove and playMoveNow is that playMove adds another move to the move queue, while playMoveNow replaces the whole move queue with new move:

 

  • Like 1

Share this post


Link to post
Share on other sites

this animation in particular ("AmovPercMstpSnonWnonDnon_Salute") is a static animation, if you go in the animation viewer, you'll that the duration of that move is 0, so you need some kind of loop to always make the unit maintain that pose,  so the waituntil in Grumpy Old Man's code wouldn't work for me, and even though you are totally right about playMove being better in the code I gave you, for my purposes I still need to use switchMove. Anyways, thanks for your response 

Share this post


Link to post
Share on other sites

waitUnil is a loop on it's own, so you could do this:

waitUntil { _unit PlayMoveNow"AmovPercMstpSnonWnonDnon_Salute"; !s2};

  • Like 1

Share this post


Link to post
Share on other sites

Yeah. As long as last returns false it will loop. As stanhope said.

  • Like 1

Share this post


Link to post
Share on other sites

Then I screwed up my code somewhere else for it to not work, I'll remember waituntil next time I'll need to use some loop, so thanks a lot for correcting me

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

×