exitWith: Difference between revisions

From Bohemia Interactive Community
Jump to navigation Jump to search
m (Text replacement - "<code>([^<]*)\[\[([a-zA-Z][a-zA-Z0-9_]+)\]\]([^<]*) *<\/code>" to "<code>$1$2$3</code>")
m (Text replacement - "\|x([0-9])= *<code>([^<]*)<\/code>" to "|x$1= <sqf>$2</sqf>")
Line 29: Line 29:
|r1= [[Anything]]
|r1= [[Anything]]


|x1= <code>systemChat "start";
|x1= <sqf>systemChat "start";
private _condition = true;
private _condition = true;


Line 48: Line 48:
};
};


systemChat "never shown - the script has already ended";</code>
systemChat "never shown - the script has already ended";</sqf>


|x2= <code>for "_j" from 1 to 10 do
|x2= <sqf>for "_j" from 1 to 10 do
{
{
systemChat format ["%1", _j];
systemChat format ["%1", _j];
Line 60: Line 60:
};
};
};
};
systemChat "Complete";</code>
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:
Line 78: Line 78:
};</code>
};</code>


|x4= <code>while { true } do
|x4= <sqf>while { true } do
{
{
if (alive player) then
if (alive player) then
Line 87: Line 87:
};
};
};
};
};</code>
};</sqf>


|x5= <code>if (_condition) exitWith
|x5= <sqf>if (_condition) exitWith
{
{
hint "reached";
hint "reached";
};
};
{{cc|else { hint "not reached" }; // '''wrong''' - using else does not work and makes no sense here}}</code>
{{cc|else { hint "not reached" }; // '''wrong''' - using else does not work and makes no sense here}}</sqf>
<code>if (_condition) exitWith
<code>if (_condition) exitWith
{
{

Revision as of 11:21, 13 May 2022

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"; }; {{cc|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 May 28, 2010 - 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 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 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.