exitWith: Difference between revisions
Jump to navigation
Jump to search
Lou Montana (talk | contribs) m (Text replacement - "<code>([^<]*)\[\[([a-zA-Z][a-zA-Z0-9_]+)\]\]([^<]*) *<\/code>" to "<code>$1$2$3</code>") |
Lou Montana (talk | contribs) m (Text replacement - "<sqf>([^↵][^<]*↵[^<]*)<\/sqf>" to "<sqf> $1 </sqf>") |
||
(27 intermediate revisions by the same user not shown) | |||
Line 19: | Line 19: | ||
|descr= Exits the '''current''' code scope. Often used for exiting [[do]], [[for]], [[count]], [[forEach]] or the whole script. | |descr= Exits the '''current''' code scope. Often used for exiting [[do]], [[for]], [[count]], [[forEach]] or the whole script. | ||
{{Feature|informative|See {{ | {{Feature|informative|See {{Link|Variables#Local Variables Scope}} for more information.}} | ||
|s1= ifType [[exitWith]] code | |s1= ifType [[exitWith]] code | ||
Line 29: | Line 29: | ||
|r1= [[Anything]] | |r1= [[Anything]] | ||
|x1= < | |x1= <sqf> | ||
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"; | |||
</sqf> | |||
|x2= < | |x2= <sqf> | ||
for "_j" from 1 to 10 do | |||
{ | { | ||
systemChat format ["%1", _j]; | |||
// the | // 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";</ | systemChat "Complete"; | ||
</sqf> | |||
|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: | |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: | ||
< | <sqf> | ||
onEachFrame { | |||
if (!alive player) exitWith | |||
{ | { | ||
onEachFrame {}; | |||
}; | }; | ||
};</ | }; | ||
</sqf> | |||
< | <sqf> | ||
_time = time + 10; | |||
waitUntil | |||
{ | { | ||
if (time > _time) exitWith { true }; | |||
false | |||
};</ | }; | ||
</sqf> | |||
|x4= < | |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"; | |||
}; | }; | ||
}; | }; | ||
};</ | }; | ||
</sqf> | |||
|x5= < | |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 "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]] | |seealso= [[scopeName]] [[breakOut]] [[breakTo]] [[else]] [[Control Structures]] [[then]] [[assert]] [[try]] [[catch]] [[throw]] | ||
Line 106: | Line 120: | ||
<dt><dt> | <dt><dt> | ||
<dd class="notedate">Posted on | <dd class="notedate">Posted on 2010-05-28 - 22:15</dd> | ||
<dt class="note">[[User: | <dt class="note">[[User:RoehrenRadio|RoehrenRadio]]</dt> | ||
<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> | <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> | ||
<dt><dt> | <dt><dt> | ||
<dd class="notedate">Posted on | <dd class="notedate">Posted on 2013-08-04 - 12:20</dd> | ||
<dt class="note">[[User:Killzone_Kid|Killzone_Kid]]</dt> | <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 | <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> | ||
<dt></dt> | <dt></dt> | ||
<dd class="notedate">Posted on | <dd class="notedate">Posted on 2016-03-10 - 08:31 (UTC)</dd> | ||
<dt class="note">[[User: | <dt class="note">[[User:Killzone_Kid|Killzone_Kid]]</dt> | ||
<dd class="note"> | <dd class="note"> | ||
[[exitWith]] cannot be used in event handlers with override ability to simply exit with override value. The following is incorrect: | [[exitWith]] cannot be used in event handlers with override ability to simply exit with override value. The following is incorrect: | ||
< | <sqf> | ||
onMapSingleClick {if (!isServer) exitWith {true}};</ | // 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: | 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> | ||
onMapSingleClick {call {if (!isServer) exitWith {true}}};</ | // 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. | [[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> | </dd> | ||
</dl> | </dl> |
Latest revision as of 19:42, 3 September 2024
Description
- Description:
- Exits the current code scope. Often used for exiting do, for, count, forEach or the whole script.
- Groups:
- Program Flow
Syntax
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:
- Example 4:
- Example 5:
Additional Information
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
- RoehrenRadio
- 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:
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: 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.
Categories:
- Scripting Commands
- Introduced with Armed Assault version 1.00
- ArmA: Armed Assault: New Scripting Commands
- ArmA: Armed Assault: Scripting Commands
- Arma 2: Scripting Commands
- Arma 2: Operation Arrowhead: Scripting Commands
- Take On Helicopters: Scripting Commands
- Arma 3: Scripting Commands
- Command Group: Program Flow