tophe 69 Posted July 20, 2010 (edited) 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 July 20, 2010 by Tophe i'm an idiot Share this post Link to post Share on other sites
snkman 351 Posted July 20, 2010 (edited) _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 July 20, 2010 by SNKMAN Share this post Link to post Share on other sites
st_dux 26 Posted July 20, 2010 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
Grizzle 0 Posted July 20, 2010 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
TRexian 0 Posted July 20, 2010 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
tophe 69 Posted July 20, 2010 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
venilsonribeiro 0 Posted January 16, 2016 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
opusfmspol 282 Posted January 16, 2016 Friends, How exit this loop... end start the game ? You need a condition. What has to happen for the loop to stop? 1 Share this post Link to post Share on other sites
venilsonribeiro 0 Posted January 17, 2016 It is the intro and the camera focus in each unit , need to intro runs for 60 seconds and back into the game. Share this post Link to post Share on other sites
opusfmspol 282 Posted January 17, 2016 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. 1 Share this post Link to post Share on other sites
venilsonribeiro 0 Posted January 17, 2016 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