Jump to content
Sign in to follow this  
tophe

Breaking a While-loop from within.

Recommended Posts

I'm making a script that will either run once or in a loop, depending on what the user put in.

The problem is that if I break the While condition within the while-loop it will not even run once.

Lets say I do this:

nul = [this, false] execVM "myscript.sqf"

_loop = _this select 1;
_run = true;

while {_run} do
{
  doThis;
  doThat;
  if (!_loop) then {_run = false};
};

Then it seems it will first check inside the while-loop and turn the _run switch off before even executing the loop - so the doTHis and doThat will not execute.

If I run it like this: nul = [this, true] execVM "myscript.sqf" it will keep looping.

I'm thinking what should happen is that it should run until the if-statement comes and then switch _run to false, go back up and realize that it should stop and then quit.

Any help appreciated..

***** edit

Now it works....

Seems I did a typo!

Edited by Tophe
i'm an idiot

Share this post


Link to post
Share on other sites

_loop = _this select 1;

if !(_loop) exitWith {};

while {True} do
{
  doThis;
  doThat;
};

You should use a sleep too?

_loop = _this select 1;

if !(_loop) exitWith {};

while {True} do
{
  doThis;
  doThat;

  sleep 1;
};

Edited by SNKMAN

Share this post


Link to post
Share on other sites

You can also use "if (condition) exitWith {}" to break out of any loop. It will not exit the entire script unless you are in the main scope.

Share this post


Link to post
Share on other sites
You can also use "if (condition) exitWith {}" to break out of any loop. It will not exit the entire script unless you are in the main scope.

+1. I know BIS says behavior of exitWith{} may be unexpected at times, but I found it works great to exit while loops.

Share this post


Link to post
Share on other sites

I have noticed that the BI scripters use scopeName and breakTo to great effect.

From the wiki:

scopeName "main";
 while {true} do {
      scopeName "loop1";
      while {true} do {
         scopeName "loop2";
          if (condition1) then {breakTo "main"}; // Breaks all scopes and return to "main"
          if (condition2) then {breakOut  "loop2"}; // Breaks scope named "loop2"
      sleep  1; };
  sleep  1; }; 

I will probably move toward this framework in my own conditional-writing. :)

Share this post


Link to post
Share on other sites

Thx guys.

Yeah SNKMAN I always use a sleep in the loops... Learned that the hard way.

Didn't know that with the exitWith. I always thought it ended the whole script.

The scope-thingy looks like a nice way to keep track of loops.

Feels a bit like doing it the old GOTO way.

Share this post


Link to post
Share on other sites

Friends, How exit this loop... end start the game ?

 

 

titlecut [" ","BLACK IN",3];
_camera = "camera" camcreate [0,0,0];
_camera cameraeffect ["external", "front"];
showCinemaBorder true;
 
while {true} do
 
{
_aliveCount = count allUnits; 
_aliveSelect = _aliveCount - 1;
_randomSelect = round random _aliveSelect;
_newTarget = allUnits select _randomSelect;
_camera camsettarget _newTarget;
_randomX = (random 6);
_randomY = (random 6);
_camera camsetrelpos [_randomX,_randomY,4];
_camera camcommit 0;
sleep (3 + (random 6));
  };

Share this post


Link to post
Share on other sites

 

Friends, How exit this loop... end start the game ?

 

You need a condition.  What has to happen for the loop to stop?

  • Like 1

Share this post


Link to post
Share on other sites

See if this works first:

 

End after mission has run for 60 seconds

while {time < 60} do
{
	_aliveCount = count allUnits; 
	_aliveSelect = _aliveCount - 1;
	_randomSelect = round random _aliveSelect;
	_newTarget = allUnits select _randomSelect;
	_camera camsettarget _newTarget;
	_randomX = (random 6);
	_randomY = (random 6);
	_camera camsetrelpos [_randomX,_randomY,4];
	_camera camcommit 0;
	sleep (3 + (random 6));
};

Having {true} as the condition is what makes it keep running.  It needs to be a condition that can return false if the condition is not met, and true when the condition is met. 

 

There are exceptions when you would want to use true; i.e. if you wanted it to end after a certain unit was viewed, that would get detected inside the loop, and then you would have to use an exitWith, or a scopeName with breakTo.

  • Like 1

Share this post


Link to post
Share on other sites

Opusfmspol> Perfect!I worked very well. After the loop stopped put the script:

 

WaitUntil camCommitted _camera {};

sleep 3;
_camera cameraeffect ["terminate", "back"];
camdestroy _camera;
 
and intro returned to the player as I wanted.
Thank you my friend, now I will be able to use these dynamic intros for various missions. A big hug from Brazil.

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
Sign in to follow this  

×