Page tree
Skip to end of metadata
Go to start of metadata

Introduced in



Description:Exits the current scope, if the specified condition returns true. If the scope is a loop (using for, forEach, while, waitUntil, and so on) or another control statement with its own scope (for example, if or switch), exitWith exits that particular scope, and continues to process the commands following it. If exitWith is called outside of any statement scope, then the current script is exited. An exitWith statement does not consider any else or elseif conditions - either of them is ignored, if used.


Syntax:if (condition) exitWith {code}
  • condition: Boolean - If true, then the current scope exits.
  • code: Code - Code that is executed before the scope exits.
Return Value:Anything


Examples:Exits the current script if the required addon is not present: if !(isClass (configFile>>"cfgVehicles">>"XXX")) exitWith {
warningMessage "Plugin XXX is required!"

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:for "_j" from 1 to 10 do {
player sideChat str _j;
if (_j==>5) exitWith {player sideChat "5 is enough"};
player sideChat "Complete";

Additional Information

See also: breakOut, breakTo, Control Structures


Posted on Oct 8, 2014
If exitWith is used within a for forspec loop, which was executed in a function, and called from an array, then the variable that precedes this call will be destroyed. (Most likely exitWith thinks it needs to destroy the temporary variable used in the surrounding for-loop, but since the for forspec loop handles its loop variable differently from the for var loop, this causes the accidental delete of the last variable on the stack.)

_fnc = {
  private ["_exit"];
  _exit = _this select 0;
  for [{_i=0},{_i<3},{_i=_i+1}] do {
    if (_i==_exit) exitWith {
      player sidechat format["   exit @ %1",_exit];

_a = "a"; _b = "b";
player sidechat format["call %1, %2, %3", _a, _b, [1] call _fnc]; // destroys "_b"; returns "call a, <null>, true"
player sidechat format["call %1, %2, %3", _a, _b, [9] call _fnc];  // retains "_b"; returns "call a, b, true"