Jump to content
Sign in to follow this  
Sibolo

Exitwith{} Command use

Recommended Posts

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

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
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
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

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
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 by Muzzleflash

Share this post


Link to post
Share on other sites

Muzzleflash hasn't posted since then... it must be working for him that exitWith.

Share this post


Link to post
Share on other sites

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

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
Sign in to follow this  

×