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

Introduced in




Runs the passed code in parallel to the current script.

  • Spawn does not wait for the called code to be finished. Once the spawn command has been executed, the calling script immediately continues with the next command, independently from whether the spawned code has completed (unlike call, which waits for the called code to finish):


    player sidechat "before spawn";
    [] spawn {
        waitUntil {!alive tank1}; // spawned code exits when 'tank1' is killed
        player sidechat "tank1 is dead";
    player sidechat "after spawn"; // this command is executed right away, whether 'tank1' has been killed or not

  • Spawn does not return the result of the code (like call does). Instead, it returns a Script (Handle) that can be evaluated by scriptDone.


    player sidechat "before spawn";
    _sqf = [] spawn { // the returned script handle (_sqf) is saved, and used with scriptDone
       waitUntil {!alive tank1}; // spawned code exits when 'tank1' is killed
    player sidechat "spawn returned"; // this line is executed right away, after the code is spawned
    waitUntil {scriptDone _sqf}; // evaluate _sqf, to determine when the spawned code is finished
    player sidechat "spawn done"; // this line is executed only once the spawned code has finished

  • Spawn does not share the variable space with the calling script (it cannot access local variables or functions, defined at that level, unlike call, which does). However, global variables and functions are still available:


    _fnc = {aaa = aaa + 1};
    _aaa = 0;
    aaa = 0;
    _sqf = [] spawn {_aaa = 1; aaa=1; [] call _fnc};
    waitUntil {scriptDone _sqf};
    player sidechat str [_aaa, aaa]; // _aaa is still 0, and aaa is 1
    // (spawn does not have access to either the variable _aaa or the function _fnc)


Syntax:arguments spawn code
  • arguments: Any Value - Elements passed here are available as _this in the spawned script. Must be present, even if it is just an empty array ([]).
  • code: Code - Code to execute.
Return Value:Script (Handle)


Examples:Called code is executed in parallel to the calling script:
[] spawn { player globalChat "This is run in parallel." }

Additional Information

See also: execVM, call, compile, preprocessFileLineNumbers, preprocessFile, terminate, scriptDone