---
name: "getVariable"
type: command
status: ok
groups: ["Variables", "Namespaces", "Multiplayer"]
arma3_since: "0.50"
locality: {"argument": "global", "effect": null, "server_exec": false}
source: https://community.bistudio.com/wiki/getVariable
revid: 369203
updated: 2024-09-03T10:34:50Z
---

# getVariable

*Command · Variables, Namespaces, Multiplayer · Arma 3 v0.50 · introduced in Armed Assault v1.00*

Locality: arguments **global**

## Description

Returns the value of variable in the variable space assigned to various data types.
All available data types combinations:

| Primary syntax ([String](../topics/String.md)) | Alternative syntax ([Array](../topics/Array.md)) |  |  |
|---|---|---|---|
| **Code** | **Since** | **Code** | **Since** |
| [Namespace](../topics/Namespace.md) getVariable [String](../topics/String.md) | - | [Namespace](../topics/Namespace.md) getVariable [Array](../topics/Array.md) | Arma 2: Operation Arrowhead v1.60 |
| [Object](../topics/Object.md) getVariable [String](../topics/String.md) | - | [Object](../topics/Object.md) getVariable [Array](../topics/Array.md) | - |
| [Group](../topics/Group.md) getVariable [String](../topics/String.md) | - | [Group](../topics/Group.md) getVariable [Array](../topics/Array.md) | - |
| [Team Member](../topics/Team_Member.md) getVariable [String](../topics/String.md) | - | [Team Member](../topics/Team_Member.md) getVariable [Array](../topics/Array.md) | - |
| [Task](../topics/Task.md) getVariable [String](../topics/String.md) | - | [Task](../topics/Task.md) getVariable [Array](../topics/Array.md) | Arma 3 v1.68 |
| [Location](../topics/Location.md) getVariable [String](../topics/String.md) | - | [Location](../topics/Location.md) getVariable [Array](../topics/Array.md) | Arma 3 v1.68 |
| [Control](../topics/Control.md) getVariable [String](../topics/String.md) | - | [Control](../topics/Control.md) getVariable [Array](../topics/Array.md) | Arma 3 v1.56 |
| [Display](../topics/Display.md) getVariable [String](../topics/String.md) | Arma 3 v1.56 | [Display](../topics/Display.md) getVariable [Array](../topics/Array.md) | Arma 3 v1.56 |

> [!IMPORTANT]
> When variable is [set](../commands/setVariable.md) on a [Task](../topics/Task.md), it is not actually set on the task itself, but on the FSM attached to the task.
>
> If there is no FSM, [getVariable](../commands/getVariable.md) will not work.

> [!NOTE]
> **Arma 3:** In the case of Alt Syntax usage in Arma 3, *defaultValue* will be returned if:
> - Requested variable is either undefined or [nil](../commands/nil.md)
> - Variable namespace is null ([objNull](../commands/objNull.md), [grpNull](../commands/grpNull.md), etc.)

## Syntax

### Syntax 1

```sqf
varspace getVariable name
```

**Parameters:**

- `varspace`: [Namespace](../topics/Namespace.md), [Object](../topics/Object.md), [Display](../topics/Display.md), [Control](../topics/Control.md), [Group](../topics/Group.md), [Location](../topics/Location.md), [Task](../topics/Task.md), [Team Member](../topics/Team_Member.md), [Display](../topics/Display.md), [Control](../topics/Control.md)
- `name`: [String](../topics/String.md) - variable name in given namespace

**Returns:** [Anything](../topics/Anything.md) or [Nothing](../topics/Nothing.md) if the variable doesn't exist

### Syntax 2

```sqf
varspace getVariable [name, defaultValue]
```

**Parameters:**

- `varspace`: [Namespace](../topics/Namespace.md), [Object](../topics/Object.md), [Display](../topics/Display.md), [Control](../topics/Control.md), [Group](../topics/Group.md), [Location](../topics/Location.md), [Task](../topics/Task.md), [Team Member](../topics/Team_Member.md), [Display](../topics/Display.md), [Control](../topics/Control.md)
- `name`: [String](../topics/String.md) - variable name in given namespace
- `defaultValue`: [Anything](../topics/Anything.md) - value to return if variable doesn't exist 

  > [!WARNING]
  > A dynamic *defaultValue* e.g `random 500` will always be executed, even if the variable is defined.

**Returns:** [Anything](../topics/Anything.md) - current value of the variable or *defaultValue* if the variable doesn't exist.

## Examples

### Example 1

```sqf
private _variable = myTruck getVariable "myVariable"; // returns nil if "myVariable" is not set
```

### Example 2

```sqf
private _variable = myTruck getVariable ["myVariable", 50]; // returns 50 if "myVariable" is not set
```

### Example 3

```sqf
for "_i" from 0 to 5 do
{
    _car = missionNamespace getVariable ("car" + str _i);
    _car setDamage 0;
}; // Set damage of car0..car5 to 0
```

### Example 4

```sqf
myMissionVar = 2015;
missionNamespace getVariable "myMissionVar"; // returns 2015
```

### Example 5

Get current value of a variable and if it is undefined, define it and get the defined value:

```sqf
private _var = missionNamespace getVariable "varName";
if (isNil "_var") then
{
    missionNamespace setVariable ["varName", 123];
    _var = 123;
};
// _var here will contain current value of the variable varName
```

## See also

[setVariable](../commands/setVariable.md) · [allVariables](../commands/allVariables.md) · [getFSMVariable](../commands/getFSMVariable.md)

## Community notes

**Ebay** — 2016-08-13

**NOTE: Arma 2 only! In Arma 3 this will return default value:**
Warning: the alternative syntax returns undefined when the namespace is an object and that object is null. Example:

```sqf
_test = objNull getVariable ["test", "0"];
systemChat _test;
```

Errors because _test is undefined. Tested in Arma 2: Operation Arrowhead 1.63.131129

---

**AgentRev** — 2017-08-31

You can do typechecking using [param](../commands/param.md):

```sqf
private _myVar = [player getVariable "myVar"] param [0, "", [""]];
```

and [params](../commands/params.md):

```sqf
[player getVariable "myVar"] params [["_myVar", "", [""]]];
```

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