---
name: "Variables"
type: topic
source: https://community.bistudio.com/wiki/Variables
revid: 376701
updated: 2025-11-07T23:34:36Z
---

# Variables

A [variable](../topics/Variables.md) is a "storage container" or "named placeholder" for data. You can read and modify the data once this container is created.

Its "name" is referenced as [Identifier](../topics/Identifier.md).

### Requirements

The following links guide to the basics to understand this article:

- Introduction to Arma Scripting
- [Identifier](../topics/Identifier.md)

### Initialisation

The first thing to do to create a variable is to find its name, also called [identifier](../topics/Identifier.md); this name must be speaking to the reader - keep in mind that code is meant to be read by *human beings* (see [Code Best Practices - Variable format](../topics/Code_Best_Practices.md#variable-format)).

Once a proper name is found, it can be used to **declare** (or **initialise**) the variable:

```sqf
myVariable = 0;
```

Before Arma 2, querying undefined (or uninitialised) variables returns [nil](../commands/nil.md) (undefined value); from Arma 2 and later, it returns an "Error Undefined variable in expression" error.

> [!NOTE]
> An undefined ([nil](../commands/nil.md)) variable converted to [String](../topics/String.md) with [str](../commands/str.md) will return scalar bool array string 0xe0ffffef (in Armed Assault) and scalar bool array string 0xfcffffef (in Operation Flashpoint).
>
> Unless trying to emulate [isNil](../commands/isNil.md), **always** declare your variable before trying to access it.

### Deletion

Once created, variables take up space in the computer's memory.

This is not drastic for small variables, but if a big number of very large variables is used, it is recommended to undefine the unneeded ones in order to free up memory.

Variable deletion is done by setting its value to [nil](../commands/nil.md):

```sqf
HugeVariable = nil;
```

> [!NOTE]
> Local variables are automatically freed (deleted from memory) when their scope is exited, avoiding the need to manually deallocate them.

### Scopes

Variables are only visible in certain scopes of the game. This prevents name conflicts between different variables in different scripts.

There are two main scopes:

#### Global Scope

A global variable is visible from all scripts on the computer on which it was defined.
Names given to units in the Mission Editor are also global variables pointing to those units, which may not be redefined or modified.

> [!IMPORTANT]
> a global variable can be different from one machine to another! To ensure the global variable's proper broadcast, see [publicVariable](../commands/publicVariable.md), [publicVariableServer](../commands/publicVariableServer.md) and [publicVariableClient](../commands/publicVariableClient.md).
> If the value changes, the broadcast must be reapplied. e.g:
>
> ```sqf
> GlobalVariable = 33;
> publicVariable "GlobalVariable";
>
> GlobalVariable = 42;                // GlobalVariable is now 42 on the current machine but still 33 on the others
> publicVariable "GlobalVariable";    // updates the value to 42 for everyone
> ```

#### Local Scope

A local variable is only visible in the Script, Function or [Control Structure](../topics/Control_Structures.md) in which it was defined.

> [!NOTE]
> A local variable cannot be broadcast. In order to broadcast a local variable's *value*, it must be assigned to a global variable first:
>
> ```sqf
> private _myLocalVariable = 33;
> publicVariable "_myLocalVariable";    // incorrect
>
> GlobalVariable = _myLocalVariable;
> publicVariable "GlobalVariable";    // correct
> ```

#### Local Variables Scope

> [!WARNING]
> Local variables in [call](../commands/call.md)able code (e.g [Functions](../topics/Functions_Library.md)) should be scoped using the [private](../commands/private.md)/[privateAll](../commands/privateAll.md) commands,
> otherwise you may modify local variables of the [call](../commands/call.md)ing script that are visible in the function.

```sqf
// Since Arma 3 v1.54
private _myLocalVariable = 0;

// From Arma 2 until Arma 3 v1.54
local _myLocalVariable = 0;

// Before Arma 2
private "_myLocalVariable";
_myLocalVariable = 0;

// Alternative method to private multiple local variables at the same time
private ["_myLocalVariable1", "_myLocalVariable2"];
_myLocalVariable1 = 1;
_myLocalVariable2 = 2;
```

If a [private](../commands/private.md) variable is initialised within a [Control Structure](../topics/Control_Structures.md) (i.e. [if](../commands/if.md), [for](../commands/for.md), [switch](../commands/switch.md), [while](../commands/while.md)),
its existence will be limited to it and its sub-structures - the variable does not exist outside of the structure and will be seen as undefined.

| Variables<br>lifespan | Code |  |  |  |  |  |  |  |  |
|---|---|---|---|---|---|---|---|---|---|
|  |  |  |  |  |  |  |  |  | **BEGINNING OF FILE** - e.g `execVM "script.sqf";` |
|  |  |  |  |  |  |  |  |  | **TAG_GlobalVariable** = "Global variable"; `// Global variable is accessible from any scope` |
|  |  |  |  |  |  |  |  |  |  |
|  |  |  | [private](../commands/private.md) **_myVariable** = 0; |  |  |  |  |  |  |
|  |  | [if](../commands/if.md) (**_condition**) [then](../commands/then.md) |  |  |  |  |  |  |  |
|  |  | { |  |  |  |  |  |  |  |
|  |  | **_myVariable** = 1; |  |  |  |  |  |  |  |
|  |  |  | [private](../commands/private.md) **_myVariable** = 2; |  |  |  |  |  |  |
|  |  | **_myVariable** = 3; |  |  |  |  |  |  |  |
|  |  |  | } [else](../commands/else.md) { |  |  |  |  |  |  |
|  |  | **_myVariable** = 4; |  |  |  |  |  |  |  |
|  |  | [private](../commands/private.md) **_anotherVariable** = 10; |  |  |  |  |  |  |  |
|  |  | }; |  |  |  |  |  |  |  |
|  |  |  |  |  |  |  |  |  |  |
|  |  | [hint](../commands/hint.md) [str](../commands/str.md) **_myVariable**;			`// if **_condition** is true,  **_myVariable**'s value is **1**;` |  |  |  |  |  |  |  |
|  |  | `// if **_condition** is false, **_myVariable**'s value is **4**.` |  |  |  |  |  |  |  |
|  |  |  |  |  |  |  |  |  |  |
|  |  | 0 [call](../commands/call.md) { |  |  |  |  |  |  |  |
|  |  | [hint](../commands/hint.md) [str](../commands/str.md) **_myVariable**;		`// works, as called code runs in the same scope` |  |  |  |  |  |  |  |
|  |  | }; |  |  |  |  |  |  |  |
|  |  |  |  |  |  |  |  |  |  |
|  |  | [**_myVariable**] [spawn](../commands/spawn.md) { |  |  |  |  |  |  |  |
|  |  |  | [hint](../commands/hint.md) **TAG_GlobalVariable**;	`// works` |  |  |  |  |  |  |
|  |  |  | [hint](../commands/hint.md) [str](../commands/str.md) _myVariable;		`// throws an "undefined variable" error,` |  |  |  |  |  |  |
|  |  |  | `// as **_myVariable** does **not** exist in this new script` |  |  |  |  |  |  |
|  |  |  | }; |  |  |  |  |  |  |
|  |  |  |  |  |  |  |  |  |  |
|  |  | `// trying to use **_anotherVariable** here would result in an "undefined variable" error,` |  |  |  |  |  |  |  |
|  |  | `// **_anotherVariable** being only scoped in the else block.` |  |  |  |  |  |  |  |
|  |  |  |  |  |  |  |  | **END OF FILE** - **TAG_GlobalVariable** keeps on living in [missionNamespace](../commands/missionNamespace.md) |  |

| Correct | Incorrect |
|---|---|
| ```sqf<br>private _living = false;<br>if (alive player) then<br>{<br>    _living = true;<br>};<br>hint format ["%1", _living]; // returns true<br>``` | ```sqf<br>// -<br>if (alive player) then<br>{<br>    private _living = true;<br>};<br>// throws an error since the private variable was<br>// not initialised outside of the if control structure.<br>hint format ["%1", _living];<br>``` |

### Data Types

Variables may store values of a certain Data Types ([String](../topics/String.md), [Number](../topics/Number.md), etc). The kind of the value specifies the *type* of the variable.
Different [operators](../topics/Operators.md) and commands require variables to be of different types.

A variable is not strongly typed and changes its type according to the new data:

```sqf
private "_myVariable";    // nil
_myVariable = 1;        // 1 (Number)
_myVariable = "test";    // "test" (String)
```

### Multiplayer Considerations

Storing functions (or any callable code) into global variables without securing them with [compileFinal](../commands/compileFinal.md) (since Arma 3) is a **very** bad practice in multiplayer.
The biggest security risk would be to see it being overridden by a malicious usage of [publicVariable](../commands/publicVariable.md), setting potentially dangerous code in it.

The best option is to declare your function in [CfgFunctions](../topics/Functions_Library.md) so the engine secures it for you.

If you want to manually create a global function anyway, the best practice is the following:

```sqf
TAG_MyGlobalVariableFunction = compileFinal preprocessFileLineNumbers "functionFile.sqf";
```

> [!NOTE]
> You should ideally run this code locally on every machine.
> Using [publicVariable](../commands/publicVariable.md)  on a "final" global function (created with [compileFinal](../commands/compileFinal.md)) will indeed broadcast the variable **and** make it final on other clients as well, but the network can quickly become saturated from sending big pieces of code.

### See also

- Introduction to Arma Scripting
- [Identifier](../topics/Identifier.md)
- Data Types
- [Control Structures](../topics/Control_Structures.md)
- [Magic Variables](../topics/Magic_Variables.md)
- [private](../commands/private.md)/[privateAll](../commands/privateAll.md) (Arma 3)
- [local](../commands/local.md) (Arma 2)

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