{"name":"spawn","operator":null,"title":"spawn","type":"command","url":"https://community.bistudio.com/wiki/spawn","revid":373975,"updated":"2025-07-10T15:33:35Z","groups":["Program Flow"],"status":"ok","replaced_by":[],"games":[{"game":"ofpe","name":"Operation Flashpoint: Elite","version":"1.00"},{"game":"arma1","name":"Armed Assault","version":"1.00"},{"game":"arma2","name":"Arma 2","version":"1.00"},{"game":"arma2oa","name":"Arma 2: Operation Arrowhead","version":"1.50"},{"game":"tkoh","name":"Take On Helicopters","version":"1.00"},{"game":"arma3","name":"Arma 3","version":"0.50"}],"since":{"game":"ofpe","name":"Operation Flashpoint: Elite","version":"1.00"},"arma3_since":"0.50","branch":null,"locality":{"argument":null,"effect":null,"server_exec":false},"exec":null,"description":"Adds given set of compiled instructions to the scheduler.\nExactly when the code will be executed is unknown, it depends on how busy is the engine and how filled up is the scheduler.\nTherefore spawn does not wait for the supplied code to finish, instead, spawn returns a Script handle to the scheduler task.\nscriptDone command can be used to check the code completion. Additional arguments are passed to the code in local variable _this.\nSince Arma 3 v1.56 the script handle also exists inside the code in `_thisScript` variable.\nTo see what spawned scripts are currently in the scheduler, use diag_activeSQFScripts command.\n\n> [!WARNING]\n> When multiple Code is spawned in an order, there is no guarantee that the spawned Code will execute in the same order (see #Example 2). If the order is important, use BIS_fnc_spawnOrdered.","syntaxes":[{"syntax":"arguments spawn code","since":null,"effect":null,"exec":null,"params":[{"name":"arguments","type":"Anything","optional":false,"default":null,"description":"arguments passed to the script, which later available in `_this` variable inside the script.","types":["Anything"],"since":null,"text":"arguments: Anything - arguments passed to the script, which later available in `_this` variable inside the script."},{"name":"code","type":"Code","optional":false,"default":null,"description":null,"types":["Code"],"since":null,"text":"code: Code"}],"returns":{"type":"Script Handle","types":["Script Handle"],"description":"can be used to determine (via scriptDone (also via isNull in Arma 3)) when the spawned script has finished. In Arma 3, the handle is also available inside the spawned script in `_thisScript` variable","text":"Script Handle - can be used to determine (via scriptDone (also via isNull in Arma 3)) when the spawned script has finished. In Arma 3, the handle is also available inside the spawned script in `_thisScript` variable"},"form":"binary","operator":null,"left":{"text":"arguments","types":["Anything"]},"right":{"text":"code","types":["Code"]}},{"syntax":"spawn code","since":"arma3 2.22","effect":null,"exec":null,"params":[{"name":"code","type":"Code","optional":false,"default":null,"description":null,"types":["Code"],"since":null,"text":"code: Code"}],"returns":{"type":"Script Handle","types":["Script Handle"],"description":null,"text":"Script Handle"},"form":"unary","operator":null,"left":null,"right":{"text":"code","types":["Code"]}},{"syntax":"spawn name","since":"arma3 2.22","effect":null,"exec":null,"params":[{"name":"name","type":"String","optional":false,"default":null,"description":"The name of a \"Empty Handle\". Behaves like scriptName inside a function.","types":["String"],"since":null,"text":"name: String - The name of a \"Empty Handle\". Behaves like scriptName inside a function."}],"returns":{"type":"Script Handle","types":["Script Handle"],"description":"A \"Empty Handle\" to be used as a Promise","text":"Script Handle - A \"Empty Handle\" to be used as a Promise"},"form":"unary","operator":null,"left":null,"right":{"text":"name","types":["String"]}}],"examples":[{"text":"```sqf\n_handle = 0 spawn { player globalChat \"Hello world!\" };\n```","code":["_handle = 0 spawn { player globalChat \"Hello world!\" };"]},{"text":"There is no guarantee that spawned scripts will be executed in the same order they spawned:\n\n```sqf\nfor \"_i\" from 0 to 100 do\n{\n    _i spawn\n    {\n        diag_log _this;\n    };\n}; // Result: 51,1,2...49,50,0,52,53...100\n```","code":["for \"_i\" from 0 to 100 do\n{\n\t_i spawn\n\t{\n\t\tdiag_log _this;\n\t};\n}; // Result: 51,1,2...49,50,0,52,53...100"]},{"text":"Local variables declared in the main scope are not available in the spawned code. You have to pass them as parameters:\n\n```sqf\nprivate _localVariable = 1234;\n[_localVariable] spawn \n{\n    systemChat format [\"_localVariable does not exist: %1\", isNil \"_localVariable\"]; // _localVariable does not exist: true\n    params [\"_localVariable\"];\n    systemChat format [\"_localVariable is now: %1\", _localVariable]; // _localVariable is now: 1234\n};\n```","code":["private _localVariable = 1234;\n[_localVariable] spawn \n{\n\tsystemChat format [\"_localVariable does not exist: %1\", isNil \"_localVariable\"]; // _localVariable does not exist: true\n\tparams [\"_localVariable\"];\n\tsystemChat format [\"_localVariable is now: %1\", _localVariable]; // _localVariable is now: 1234\n};"]}],"multiplayer":null,"problems":null,"aliases":[],"see_also":["call","execVM","execFSM","exec","compile","preprocessFileLineNumbers","preprocessFile","terminate","scriptDone","remoteExec","sleep","uiSleep","canSuspend"],"notes":[{"user":"Kronzky","date":"2009-03-06T00:20:00Z","text":"spawn cannot call other local functions on the same scope as itself.\n\nIt can, however, call other global functions:\n\n```sqf\n_addOne = { TST = TST + 1 };\nTST_addOne = { TST = TST + 1 };\n\n_add = {\n    TST = TST+1;\n    player sideChat format [\"added: %1\",TST];\n    call _addOne;\n    player sideChat format [\"called local: %1\",TST];\n    call TST_addOne;\n    player sideChat format [\"called global: %1\",TST];\n};\nTST = 0;\ncall _add;\n0 spawn _add;\n```\n\nThe call of _addOne from the spawned function does not do anything."},{"user":"DreadedEntity","date":"2014-10-21T23:33:00Z","text":"Arma 3 v0.50 spawn requires a script handle when used in the 2D-Editor.\n\nArma 3 v2.04 In Eden Editor this is no longer necessary.\n\nIn scripts and in the debug console, it is not required, but very useful for keeping track of running scripts. Having a script handle also makes it easy to terminate scripts at any time.\n\nSince spawn creates a new scheduled environment, having an excess of open threads can make the scheduler queue extremely long, significantly increasing the execution time of each thread. (it takes an extremely large amount of threads, though)"},{"user":"Nelis75733126","date":"2015-08-25T13:39:00Z","text":"If you have a local (private) function that you want to access from within code created with `spawn`, you can pass that private function to the code of `spawn`, like so:\n\n```sqf\n_someFunction = {};\n[ _someFunction ] spawn { call( _this select 0 ) };\n```"}],"categories":["ArmA: Armed Assault: Scripting Commands","Arma 2: Operation Arrowhead: Scripting Commands","Arma 2: Scripting Commands","Arma 3: Scripting Commands","Command Group: Program Flow","Introduced with Operation Flashpoint: Elite version 1.00","Operation Flashpoint: Elite: New Scripting Commands","Operation Flashpoint: Elite: Scripting Commands","Scripting Commands","Take On Helicopters: Scripting Commands"]}