exitWith: Difference between revisions

From Bohemia Interactive Community
Jump to navigation Jump to search
(xhanged syntax description to match similar commands like 'do' or 'then')
(clarified description, moving confusing note to talk)
Line 7: Line 7:
____________________________________________________________________________________________
____________________________________________________________________________________________


| If the result of condition is [[true]], code is evaluated, and current block with result of code. It only exits the current scope.
| Exits current scope {...} it is executed from, creates new scope {...code...} and executes the given code in it. Often used for exiting [[do]], [[for]], [[count]] or [[forEach]]. Exiting [[waitUntil]] or [[onEachFrame]] scopes with [[exitWith]] will have no effect as these scopes are called repeatedly and require different handling to terminate. |= Description
 
exitWith exits the execution of a loop defined by one of commands [[do]], [[for]], [[count]] or [[forEach]]. When you use exitWith not inside a loop, 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. |= Description
____________________________________________________________________________________________
____________________________________________________________________________________________


| if (condition) '''exitWith''' code |= Syntax
| [[if]] (condition) '''exitWith''' code |= Syntax


|p1= if (condition): [[If Type]] |= Parameter 1
|p1= [[if]] (condition): [[If Type]] |= Parameter 1
|p2= code: [[Code]] |= Parameter 2
|p2= code: [[Code]] |= Parameter 2


Line 81: Line 77:
[[hint]] "Mission Started"; // This code will never execute</code>
[[hint]] "Mission Started"; // This code will never execute</code>


<dd class="notedate">Posted on March 02, 2014 - 09:51
<dt class="note">'''[[User:Bluechip_arma|bluechip]]'''
<dd class="note">Do not use this command to exit a script, function, or any other scope besides a loop, as documented. While the current implementation may exit other scopes, as [[User:Killzone_Kid|Killzone_Kid]] said, it's not part of its specification and may change in the future which will break any scripts that depend on that behavior. That is what the developer was saying in the article.
<dd class="note">Also, since this command has some unintended behavior, as [[User:NaughDylan|Naught]] pointed out, you should only use this command in the immediate scope of a loop (ie not within nested blocks within the loop), until its implementation is fixed.


<!-- Note Section END -->
<!-- Note Section END -->

Revision as of 00:53, 10 March 2016

-wrong parameter ("Arma") defined!-1.00
Hover & click on the images for description

Description

Description:
Exits current scope {...} it is executed from, creates new scope {...code...} and executes the given code in it. Often used for exiting do, for, count or forEach. Exiting waitUntil or onEachFrame scopes with exitWith will have no effect as these scopes are called repeatedly and require different handling to terminate.
Groups:
Uncategorised

Syntax

Syntax:
if (condition) exitWith code
Parameters:
if (condition): If Type
code: Code
Return Value:
Anything

Examples

Example 1:
if (_x>5) exitWith {echo "_x is too big"; _x} When _x is greater than 5, outputs message and terminates code in current level with value of _x.
Example 2:
for "_j" from 1 to 10 do { player sideChat format ["%1",_j]; if (_j==5) exitWith {player sideChat "5 is enough"}; }; player sideChat "Complete"; Only the "for" loop will exit when the exitWith condition has been fulfilled (not the whole script). Execution will continue after the end of the loop (player sideChat "Complete").
Example 3:
Most loops will also terminate when their scope exited. To exit and terminate scopes which are called every frame such as onEachFrame and waitUntil use the following examples: onEachFrame { if (!alive player) exitWith { onEachFrame {} } } _time = time + 10; waitUntil { if (time > _time) exitWith { true } }

Additional Information

See also:
scopeNamebreakOutbreakToelseControl Structuresthen , assert

Notes

Report bugs on the Feedback Tracker and/or discuss them on the Arma Discord or on the Forums.
Only post proven facts here! Add Note

Notes

Posted on May 28, 2010 - 22:15
Roehre
Since ArmA2 uses Blocks in FSM as any ordinary Handle like while, for etc. in Scripts, ExitWith also only closes the Block in the FSM.
Posted on August 04, 2013 - 12:20
Killzone_Kid
The command will exit the current scope - no ifs no buts. If the current scope is a loop, it will exit the loop. If the current scope is the main body of a script, it will exit the script. For more understanding of scopes and exitWith have a look at this resource.
Posted on January 06, 2014 - 13:41
Naught
To further explain Killzone_Kid's above statement, exitWith does not work as described within any subsequent inner scopes of a loop - it will only simply exit the current scope. For example, this code will not exit the while loop:
while {true} do { // 'while' scope if (player == (leader player)) then { // 'if-then' scope if (time > (5 * 60)) exitWith {}; // This will only exit the 'if-then' scope }; sleep 1; }; hint "Mission Started"; // This code will never execute

Bottom Section