---
name: "spawn"
type: command
status: ok
groups: ["Program Flow"]
arma3_since: "0.50"
locality: {"argument": null, "effect": null, "server_exec": false}
source: https://community.bistudio.com/wiki/spawn
revid: 373975
updated: 2025-07-10T15:33:35Z
---

# spawn

*Command · Program Flow · Arma 3 v0.50 · introduced in Operation Flashpoint: Elite v1.00*

## Description

Adds given set of compiled instructions to the [scheduler](../topics/Scheduler.md).
Exactly when the code will be executed is unknown, it depends on how busy is the engine and how filled up is the [scheduler](../topics/Scheduler.md).
Therefore [spawn](../commands/spawn.md) does not wait for the supplied code to finish, instead, [spawn](../commands/spawn.md) returns a [Script](../topics/Script_Handle.md) handle to the [scheduler](../topics/Scheduler.md) task.
[scriptDone](../commands/scriptDone.md) command can be used to check the code completion. Additional arguments are passed to the code in local variable [_this](../topics/Magic_Variables.md#this).
Since Arma 3 v1.56 the script handle also exists inside the code in `_thisScript` variable.
To see what spawned scripts are currently in the scheduler, use [diag_activeSQFScripts](../commands/diag_activeSQFScripts.md) command.

> [!WARNING]
> When multiple [Code](../topics/Code.md) is [spawn](../commands/spawn.md)ed in an order, there is no guarantee that the [spawn](../commands/spawn.md)ed [Code](../topics/Code.md) will execute in the same order (see [#Example 2](../commands/spawn.md#example-2)). If the order is important, use [BIS_fnc_spawnOrdered](../functions/BIS_fnc_spawnOrdered.md).

## Syntax

### Syntax 1

```sqf
arguments spawn code
```

**Parameters:**

- `arguments`: [Anything](../topics/Anything.md) - arguments passed to the script, which later available in `_this` variable inside the script.
- `code`: [Code](../topics/Code.md)

**Returns:** [Script Handle](../topics/Script_Handle.md) - can be used to determine (via [scriptDone](../commands/scriptDone.md) (also via [isNull](../commands/isNull.md) in Arma 3)) when the spawned script has finished. In Arma 3, the handle is also available inside the spawned script in `_thisScript` variable

### Syntax 2

*since arma3 2.22*

```sqf
spawn code
```

**Parameters:**

- `code`: [Code](../topics/Code.md)

**Returns:** [Script Handle](../topics/Script_Handle.md)

### Syntax 3

*since arma3 2.22*

```sqf
spawn name
```

**Parameters:**

- `name`: [String](../topics/String.md) - The name of a "Empty Handle". Behaves like [scriptName](../commands/scriptName.md) inside a function.

**Returns:** [Script Handle](../topics/Script_Handle.md) - A "Empty Handle" to be used as a [Promise](../topics/Script_Handle.md#promise_handles)

## Examples

### Example 1

```sqf
_handle = 0 spawn { player globalChat "Hello world!" };
```

### Example 2

There is no guarantee that spawned scripts will be executed in the same order they spawned:

```sqf
for "_i" from 0 to 100 do
{
    _i spawn
    {
        diag_log _this;
    };
}; // Result: 51,1,2...49,50,0,52,53...100
```

### Example 3

Local variables declared in the main scope are not available in the spawned code. You have to pass them as parameters:

```sqf
private _localVariable = 1234;
[_localVariable] spawn 
{
    systemChat format ["_localVariable does not exist: %1", isNil "_localVariable"]; // _localVariable does not exist: true
    params ["_localVariable"];
    systemChat format ["_localVariable is now: %1", _localVariable]; // _localVariable is now: 1234
};
```

## See also

[call](../commands/call.md) · [execVM](../commands/execVM.md) · [execFSM](../commands/execFSM.md) · [exec](../commands/exec.md) · [compile](../commands/compile.md) · [preprocessFileLineNumbers](../commands/preprocessFileLineNumbers.md) · [preprocessFile](../commands/preprocessFile.md) · [terminate](../commands/terminate.md) · [scriptDone](../commands/scriptDone.md) · [remoteExec](../commands/remoteExec.md) · [sleep](../commands/sleep.md) · [uiSleep](../commands/uiSleep.md) · [canSuspend](../commands/canSuspend.md)

## Community notes

**Kronzky** — 2009-03-06

[spawn](../commands/spawn.md) cannot call other local functions on the same scope as itself.

It can, however, call other global functions:

```sqf
_addOne = { TST = TST + 1 };
TST_addOne = { TST = TST + 1 };

_add = {
    TST = TST+1;
    player sideChat format ["added: %1",TST];
    call _addOne;
    player sideChat format ["called local: %1",TST];
    call TST_addOne;
    player sideChat format ["called global: %1",TST];
};
TST = 0;
call _add;
0 spawn _add;
```

The call of _addOne from the spawned function does not do anything.

---

**DreadedEntity** — 2014-10-21

Arma 3 v0.50 [spawn](../commands/spawn.md) requires a script handle when used in the 2D-Editor.

Arma 3 v2.04 In Eden Editor this is no longer necessary.

In 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](../commands/terminate.md) scripts at any time.

Since [spawn](../commands/spawn.md) creates a new [scheduled environment](../topics/Scheduler.md#scheduled_environment), having an excess of open [threads](../topics/Scheduler.md#threads) can make the [scheduler](../topics/Scheduler.md#scheduler) queue extremely long, significantly increasing the execution time of each [thread](../topics/Scheduler.md#threads). (it takes an extremely large amount of [threads](../topics/Scheduler.md#threads), though)

---

**Nelis75733126** — 2015-08-25

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:

```sqf
_someFunction = {};
[ _someFunction ] spawn { call( _this select 0 ) };
```

<sub>Source: [BI Community Wiki — spawn](https://community.bistudio.com/wiki/spawn) (revision 373975).</sub>
