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')
m (Text replacement - "Killzone Kid" to "Killzone_Kid")
(104 intermediate revisions by 6 users not shown)
Line 1: Line 1:
{{Command|= Comments
{{RV|type=command
____________________________________________________________________________________________


| arma |= Game name
|game1= arma1
|version1= 1.00


|1.00|= Game version
|game2= arma2
____________________________________________________________________________________________
|version2= 1.00


| If the result of condition is [[true]], code is evaluated, and current block with result of code. It only exits the current scope.
|game3= arma2oa
|version3= 1.50


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.
|game4= tkoh
|version4= 1.00


It exits the loop only, not the script. |= Description
|game5= arma3
____________________________________________________________________________________________
|version5= 0.50


| if (condition) '''exitWith''' code |= Syntax
|gr1= Program Flow


|p1= if (condition): [[If Type]] |= Parameter 1
|descr= Exits the '''current''' code scope. Often used for exiting [[do]], [[for]], [[count]], [[forEach]] or the whole script.
|p2= code: [[Code]] |= Parameter 2
{{Feature|informative|See {{Link|Variables#Local Variables Scope}} for more information.}}


| [[Anything]] |= Return value
|s1= ifType [[exitWith]] 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
|p1= ifType: [[If Type]]


|x2= <code>[[for var|for]] "_j" [[from]] 1 [[to]] 10 [[do]]
|p2= code: [[Code]]
 
|r1= [[Anything]]
 
|x1= <sqf>systemChat "start";
private _condition = true;
 
if (_condition) then
{
{
[[player]] [[sideChat]] [[format]] ["%1",_j];
if (true) exitWith
if (_j<nowiki>=</nowiki><nowiki>=</nowiki>5) [[exitWith]] {[[player]] [[sideChat]] "5 is enough"};
{
systemChat "exiting if _condition scope";
};
systemChat "never shown";
};
};
[[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").
|= 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:
systemChat "exiting #1 worked";


<code>[[onEachFrame]] &#123;
if (true) exitWith
[[if]] (![[alive]] [[player]]) [[exitWith]] &#123;
{
[[onEachFrame]] &#123;&#125;
systemChat "exiting the main scope = leaving the whole script";
&#125;
};
&#125;
</code>


<code>_time = [[time]] + 10;
systemChat "never shown - the script has already ended";</sqf>
[[waitUntil]] &#123;
[[if]] ([[time]] > _time) [[exitWith]] &#123;
[[true]]
&#125;
&#125;
</code>
|= Example 3
____________________________________________________________________________________________


| [[scopeName]], [[breakOut]], [[breakTo]], [[else]], [[Control Structures]], [[then]] , [[assert]] |= See also
|x2= <sqf>for "_j" from 1 to 10 do
{
}}
systemChat format ["%1", _j];


<h3 style="display:none">Notes</h3>
// the for loop will cease and code execution will continue after the end of the loop
<dl class="command_description">
if (_j == 5) exitWith
<!-- Note Section BEGIN -->
{
systemChat "5 is enough";
};
};
systemChat "Complete";</sqf>


<dd class="notedate">Posted on May 28, 2010 - 22:15
|x3= Most loops will also terminate when their scope is exited. To exit and terminate scopes which are called every frame such as [[onEachFrame]] and [[waitUntil]] use the following examples:
<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="notedate">Posted on August 04, 2013 - 12:20
<sqf>onEachFrame {
<dt class="note">'''[[User:Killzone_Kid|Killzone_Kid]]'''
if (!alive player) exitWith
<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].
{
onEachFrame {};
};
};</sqf>


<dd class="notedate">Posted on January 06, 2014 - 13:41
<sqf>_time = time + 10;
<dt class="note">'''[[User:NaughDylan|Naught]]'''
waitUntil
<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
if (time > _time) exitWith { true };
[[if]] ([[player]] == ([[leader]] [[player]])) [[then]] { // 'if-then' scope
false
[[if]] ([[time]] > (5 * 60)) [[exitWith]] {}; // This will only exit the 'if-then' scope
};</sqf>
 
|x4= <sqf>while { true } do
{
if (alive player) then
{
if (time > 300) exitWith // [wrong] - it will only leave the "if alive player" scope, remaining in the "while true" loop forever
{
hint "exiting";
};
};
};
[[sleep]] 1;
};</sqf>
 
|x5= <sqf>if (_condition) exitWith
{
hint "reached";
};
// else { hint "not reached" }; // [wrong] - using else does not work and makes no sense here</sqf>
<sqf>if (_condition) exitWith
{
hint "reached";
};
};
[[hint]] "Mission Started"; // This code will never execute</code>
hint "not reached"; // [correct] - if _condition is met, the scope has already been exited by now</sqf>
 
|seealso= [[scopeName]] [[breakOut]] [[breakTo]] [[else]] [[Control Structures]] [[then]] [[assert]] [[try]] [[catch]] [[throw]]
}}
 
<dl class="command_description">


<dd class="notedate">Posted on March 02, 2014 - 09:51
<dt><dt>
<dt class="note">'''[[User:Bluechip_arma|bluechip]]'''
<dd class="notedate">Posted on 2010-05-28 - 22:15</dd>
<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.
<dt class="note">[[User:Roehre|Roehre]]</dt>
<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.
<dd class="note">Since Arma 2 uses Blocks in FSM as any ordinary Handle like [[while]], [[for]] etc. in Scripts, '''ExitWith''' also only closes the Block in the FSM.</dd>


<!-- Note Section END -->
<dt><dt>
</dl>
<dd class="notedate">Posted on 2013-08-04 - 12:20</dd>
<dt class="note">[[User:Killzone_Kid|Killzone_Kid]]</dt>
<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 {{Link|http://killzonekid.com/arma-scripting-tutorials-scopes/|this resource}}.</dd>


<h3 style="display:none">Bottom Section</h3>
<dt></dt>
<dd class="notedate">Posted on 2016-03-10 - 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:
<sqf>// INCORRECT USAGE
onMapSingleClick { if (!isServer) exitWith { true } };</sqf>
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:
<sqf>// CORRECT USAGE
onMapSingleClick { call { if (!isServer) exitWith { true } } };</sqf>
[[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>


[[Category:Scripting Commands|EXITWITH]]
</dl>
[[Category:Scripting Commands ArmA|EXITWITH]]
[[Category:Scripting Commands ArmA2|{{uc:{{PAGENAME}}}}]]
[[Category:Scripting Commands Arma 3|{{uc:{{PAGENAME}}}}]]
[[Category:Scripting_Commands_Take_On_Helicopters|{{uc:{{PAGENAME}}}}]]
[[Category:Command Group: Program Flow|EXITWITH]]

Revision as of 14:48, 12 March 2024

Hover & click on the images for description

Description

Description:
Exits the current code scope. Often used for exiting do, for, count, forEach or the whole script.
See Variables - Local Variables Scope for more information.
Groups:
Program Flow

Syntax

Syntax:
ifType exitWith code
Parameters:
ifType: If Type
code: Code
Return Value:
Anything

Examples

Example 1:
systemChat "start"; private _condition = true; if (_condition) then { if (true) exitWith { systemChat "exiting if _condition scope"; }; systemChat "never shown"; }; systemChat "exiting #1 worked"; if (true) exitWith { systemChat "exiting the main scope = leaving the whole script"; }; systemChat "never shown - the script has already ended";
Example 2:
for "_j" from 1 to 10 do { systemChat format ["%1", _j]; // the for loop will cease and code execution will continue after the end of the loop if (_j == 5) exitWith { systemChat "5 is enough"; }; }; systemChat "Complete";
Example 3:
Most loops will also terminate when their scope is 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 };
Example 4:
while { true } do { if (alive player) then { if (time > 300) exitWith // [wrong] - it will only leave the "if alive player" scope, remaining in the "while true" loop forever { hint "exiting"; }; }; };
Example 5:
if (_condition) exitWith { hint "reached"; }; // else { hint "not reached" }; // [wrong] - using else does not work and makes no sense here
if (_condition) exitWith { hint "reached"; }; hint "not reached"; // [correct] - if _condition is met, the scope has already been exited by now

Additional Information

See also:
scopeName breakOut breakTo else Control Structures then assert try catch throw

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
Posted on 2010-05-28 - 22:15
Roehre
Since Arma 2 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 2013-08-04 - 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 2016-03-10 - 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.