Sibolo 10 Posted February 22, 2012 I often come across this line in scripts: if (true) exitWith {}; especially at the end of the script as the last line of code. Wouldn't the script end anyway? Why is it added? Other times this command is used in the following way: if (!isServer) exitWith {}; to limit the rest of the execution of the script only on non server machines. Reading from the Biki about the Exitwith command it is clearly stated that: exitWith exits the execution of a loop defined by one of commands do, for, count or forEach. When you use exitWith not inside a loops, the behaviour is undefined - sometimes it may exit the script, sometimes some other scope, but this is not intended and designed behaviour of this command, and it is not guaranteed to work reliably. It exits the loop only, not the script. So why is this so widely used, and what would be the "correct" way to end script execution? Thank you. Share this post Link to post Share on other sites
.kju 3105 Posted February 22, 2012 exitWith {}; at the end of a script is obsolete. In between code it exits the current scope - which might be the script altogether. The alternative to this is to use if-then conditions. Share this post Link to post Share on other sites
Muzzleflash 108 Posted February 22, 2012 Reading from the Biki about the Exitwith command it is clearly stated that: I asked a similar response about exitWith quite some time ago (or read it on the tracker or something). The response I got was that even though the BIKI says it only works for loop and is not reliable in other context - it, however, does appear to work properly in other situations where you would normally have a scope, like while, and if-statements. So why is this so widely used, and what would be the "correct" way to end script execution? There is nothing incorrect about it as long as you use the construct properly. If you believe the description on the BIKI you should not use it inside while loops since that will not be "correct". Using it in a forEach would be fine. In my experience it works flawlessly with if-statements too - however, I almost never do that since you only really gain any significant performance if inside a loop at the cost of readability. Share this post Link to post Share on other sites
xeno 184 Posted February 22, 2012 I asked a similar response about exitWith quite some time ago (or read it on the tracker or something). The response I got was that even though the BIKI says it only works for loop and is not reliable in other context - it, however, does appear to work properly in other situations where you would normally have a scope, like while, and if-statements. One situation where it does not work reliably is the following: someobject addEventhandler ["handleDamage", { if (something) exitWith {0}; // more code }]; The exitWith does nothing in that case, more code is always executed. Afair there is also a CIT ticket about the problem. Xeno Share this post Link to post Share on other sites
Sibolo 10 Posted February 22, 2012 I understand that it does work in many cases to exit fom scripts and so it is widely used, but are there any alternatives? Maybe ones that will work inside EH? Share this post Link to post Share on other sites
Muzzleflash 108 Posted February 22, 2012 (edited) I understand that it does work in many cases to exit fom scripts and so it is widely used, but are there any alternatives?Maybe ones that will work inside EH? I don't think there is any - quit this scope or script command - that does what you want. You can however, workaround using nested if-statements. Xeno's example can be rewritten to work the same (assuming the exitWith worked) like this using ifs: someobject addEventhandler ["handleDamage", { if (something) then { 0 } else { //Continue doing something }; }]; Hmm post count is 666. Edited February 22, 2012 by Muzzleflash Share this post Link to post Share on other sites
das attorney 840 Posted February 22, 2012 if (_post_count == 666) exitWith {preternaturalFear.sqf}; Share this post Link to post Share on other sites
kremator 993 Posted February 23, 2012 Nice one DA ! Share this post Link to post Share on other sites
gammadust 12 Posted February 23, 2012 Muzzleflash hasn't posted since then... it must be working for him that exitWith. Share this post Link to post Share on other sites
Rkillah-Rye 10 Posted May 12, 2012 I have been playing about with the exitWith statement in a Script and found that it works best passing variables to statements lower down in the script. But I found that it will not perform things such as addaction or to run another script to addaction and passing the variables, this may be me being a dumbass but I think its because it is still within the "while" "for" "if" scope so I did this. while {Loop_Statement} do { //loop code to execute vartoPass = blah blah if(Exit_Statement) exitwith{exited=true;passVar=varToPass};//exit loop and pass new variable to lower if statement you can pass several variables including the ones from within the "while" "for" "if" scope. }; if(exited)then{ //code to execute [object] addaction [("<t color='#FF4040'>" + "Title" + "</t>"), "script.sqf",passVar]; }else{ //code to execute if loop completes }; I found this a way to pass variables from within a while statement in one script to another script via an action added to an object. The variables within the loop scope need to be passed out to the script below the loop then passed to whatever, im going to shut up now lol. If I am wrong I stand corrected because I am new to scripting for Arma2. later RK Share this post Link to post Share on other sites