exitWith: Difference between revisions

From Bohemia Interactive Community
Jump to navigation Jump to search
No edit summary
m (Text replacement - "[[Category:Scripting Commands ArmA|" to "[[Category:Scripting Commands Armed Assault|")
(29 intermediate revisions by 9 users not shown)
Line 1: Line 1:
{{Command|= Comments
{{Command|Comments=
____________________________________________________________________________________________
____________________________________________________________________________________________


| arma |= Game name
| arma |Game name=


|1.00|= Game version
|1.00|Game version=
____________________________________________________________________________________________
____________________________________________________________________________________________


| 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 if condition evaluates [[true]], creates new scope {...code...} and executes the given code in it. Often used for exiting [[do]], [[for]], [[count]] or [[forEach]]. Simply exiting [[waitUntil]] or [[onEachFrame]] scopes with [[exitWith]] will have no effect as these scopes are called repeatedly by the engine and require different handling to terminate (see Example 3). |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 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. |= Description
____________________________________________________________________________________________
____________________________________________________________________________________________


| if (condition) '''exitWith''' '''{'''[[Code]]'''}''' |= Syntax
| ifType [[exitWith]] code |Syntax=


|p1= [[If Type]] |= Parameter 1
|p1= ifType: [[If Type]] |Parameter 1=
|p2= [[Code]] |= Parameter 2
|p2= code: [[Code]] |Parameter 2=


| [[Anything]] |= Return value
| [[Anything]] |Return value=
____________________________________________________________________________________________
____________________________________________________________________________________________
   
   
|x1= <code>[[if]] (_x>5) [[exitWith]] {[[echo]] "_x is too big"; _x}</code>
|x1= <code>[[if]] (_x>5) [[exitWith]] {[[echo]] "_x is too big"; _x}</code>


When _x is greater than 5, outputs message and terminates code in current level with value of _x |= Example 1
When _x is greater than 5, outputs message and terminates code in current level with value of _x. |Example 1=


|x2= <code>[[for var|for]] "_j" [[from]] 1 [[to]] 10 [[do]]
|x2= <code>[[for]] "_j" [[from]] 1 [[to]] 10 [[do]]
{
{
[[player]] [[sideChat]] [[format]] ["%1",_j];
[[player]] [[sideChat]] [[format]] ["%1",_j];
Line 33: Line 29:
[[player]] [[sideChat]] "Complete";</code>
[[player]] [[sideChat]] "Complete";</code>
''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").
''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 2
|Example 2=


|x3= 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:
|x3= 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:
Line 45: Line 41:


<code>_time = [[time]] + 10;
<code>_time = [[time]] + 10;
[[waitUntil]] &#123;
[[waitUntil]]  
[[if]] ([[time]] > _time) [[exitWith]] &#123;
{
[[true]]
[[if]] ([[time]] > _time) [[exitWith]] { [[true]] };
&#125;
[[false]]
&#125;
}
</code>
</code>
|= Example 3
|Example 3=
____________________________________________________________________________________________
____________________________________________________________________________________________


| [[scopeName]], [[breakOut]], [[breakTo]], [[else]], [[Control Structures]], [[then]] , [[assert]] |= See also
| [[scopeName]], [[breakOut]], [[breakTo]], [[else]], [[Control Structures]], [[then]], [[assert]], [[try]], [[catch]], [[throw]] |See also=
   
   
}}
}}
Line 63: Line 59:


<dd class="notedate">Posted on May 28, 2010 - 22:15
<dd class="notedate">Posted on May 28, 2010 - 22:15
<dt class="note">'''[[User:Roehre|Roehre]]'''
<dt class="note">[[User:Roehre|Roehre]]
<dd class="note">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.
<dd class="note">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.


<dd class="notedate">Posted on August 04, 2013 - 12:20
<dd class="notedate">Posted on August 04, 2013 - 12:20
<dt class="note">'''[[User:Killzone_Kid|Killzone_Kid]]'''
<dt class="note">[[User:Killzone_Kid|Killzone_Kid]]
<dd class="note">I cannot dare to edit the description of this command as it was put by BIS dev himself. But I'd like to clarify the nature of this command to avoid any further confusion. 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 [http://killzonekid.com/arma-scripting-tutorials-scopes/ this resource].
<dd class="note">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 [http://killzonekid.com/arma-scripting-tutorials-scopes/ this resource].


<dd class="notedate">Posted on January 06, 2014 - 13:41
<dd class="notedate">Posted on January 06, 2014 - 13:41
<dt class="note">'''[[User:NaughDylan|Naught]]'''
<dt class="note">[[User:NaughDylan|Naught]]
<dd class="note">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:
<dd class="note">To further explain [[User:Killzone_Kid|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:
<dd class="note"><code>[[while]] {[[true]]} [[do]] { // 'while' scope
<dd class="note"><code>[[while]] {[[true]]} [[do]] { // 'while' scope
[[if]] ([[player]] == ([[leader]] [[player]])) [[then]] { // 'if-then' scope
[[if]] ([[player]] == ([[leader]] [[player]])) [[then]] { // 'if-then' scope
Line 80: Line 76:
};
};
[[hint]] "Mission Started"; // This code will never execute</code>
[[hint]] "Mission Started"; // This code will never execute</code>


<!-- Note Section END -->
<!-- Note Section END -->
Line 86: Line 83:
<h3 style="display:none">Bottom Section</h3>
<h3 style="display:none">Bottom Section</h3>


[[Category:Scripting Commands|EXITWITH]]
[[Category:Scripting Commands|{{uc:{{PAGENAME}}}}]]
[[Category:Scripting Commands ArmA|EXITWITH]]
[[Category:Scripting Commands Armed Assault|{{uc:{{PAGENAME}}}}]]
[[Category:Scripting Commands ArmA2|{{uc:{{PAGENAME}}}}]]
[[Category:Scripting Commands Arma 2|{{uc:{{PAGENAME}}}}]]
[[Category:Scripting Commands Take On Helicopters|{{uc:{{PAGENAME}}}}]]
[[Category:Scripting Commands Arma 3|{{uc:{{PAGENAME}}}}]]
[[Category:Scripting Commands Arma 3|{{uc:{{PAGENAME}}}}]]
[[Category:Scripting_Commands_Take_On_Helicopters|{{uc:{{PAGENAME}}}}]]
[[Category:Command Group: Program Flow|{{uc:{{PAGENAME}}}}]]
[[Category:Command Group: Program Flow|EXITWITH]]
 
<!-- CONTINUE Notes -->
<dl class="command_description">
<dd class="notedate">Posted on March 10, 2016 - 08:31 (UTC)</dd>
<dt class="note">[[User:Killzone Kid|Killzone Kid]]</dt>
<dd class="note">
[[exitWith]] cannot be used in event handlers with override ability to simply exit with override value. The following is incorrect:
<code>// INCORRECT USAGE
[[onMapSingleClick]] {[[if]] (![[isServer]]) [[exitWith]] {[[true]]}};</code>
The override value must be returned in the main scope of EH, but since it is exited with [[exitWith]], it never happens. The correct way in this case would be:
<code>// CORRECT USAGE
[[onMapSingleClick]] {[[call]] {[[if]] (![[isServer]]) [[exitWith]] {[[true]]}}};</code>
[[exitWith]] will exit current [[call]] scope only and override value therefore will appear in the main scope of the EH, right where we want it.
</dd>
</dl>
<!-- DISCONTINUE Notes -->
 
<!-- CONTINUE Notes -->
<dl class="command_description">
<dd class="notedate">Posted on October 12, 2016 - 17:56 (UTC)</dd>
<dt class="note">[[User:RHfront|RHfront]]</dt>
<dd class="note"> exitWith does not use standard "if-then" syntax, but a unique "if-exitWith" structure.
<dd class="note"> exitWith will NOT take [[else]]. Attempts to do so will result in "Error exitwith: Type Array, expected code".
</dd>
</dl>
<!-- DISCONTINUE Notes -->

Revision as of 20:00, 3 June 2020

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

Description

Description:
Exits current scope {...} it is executed from if condition evaluates true, creates new scope {...code...} and executes the given code in it. Often used for exiting do, for, count or forEach. Simply exiting waitUntil or onEachFrame scopes with exitWith will have no effect as these scopes are called repeatedly by the engine and require different handling to terminate (see Example 3).
Groups:
Uncategorised

Syntax

Syntax:
ifType exitWith code
Parameters:
ifType: 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 }; false }

Additional Information

See also:
scopeNamebreakOutbreakToelseControl Structuresthenasserttrycatchthrow

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

Posted on March 10, 2016 - 08:31 (UTC)
Killzone Kid
exitWith cannot be used in event handlers with override ability to simply exit with override value. The following is incorrect: // INCORRECT USAGE onMapSingleClick {if (!isServer) exitWith {true}}; The override value must be returned in the main scope of EH, but since it is exited with exitWith, it never happens. The correct way in this case would be: // CORRECT USAGE onMapSingleClick {call {if (!isServer) exitWith {true}}}; exitWith will exit current call scope only and override value therefore will appear in the main scope of the EH, right where we want it.
Posted on October 12, 2016 - 17:56 (UTC)
RHfront
exitWith does not use standard "if-then" syntax, but a unique "if-exitWith" structure.
exitWith will NOT take else. Attempts to do so will result in "Error exitwith: Type Array, expected code".