---
name: "setVariable"
type: command
status: ok
groups: ["Variables", "Namespaces", "Multiplayer"]
arma3_since: "0.50"
locality: {"argument": "global", "effect": "local", "server_exec": false}
source: https://community.bistudio.com/wiki/setVariable
revid: 369355
updated: 2024-09-03T18:43:04Z
---

# setVariable

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

Locality: arguments **global** · effect **local**

## Description

Set variable to given value in the variable space of given element. Can be used to broadcast variables over the network.

To remove a variable, set it to [nil](../commands/nil.md) (see [#Example 5](../commands/setVariable.md#example-5)) - note that this does not work on [Object](../topics/Object.md) and [scripted](../commands/createLocation.md) [Location](../topics/Location.md) namespaces (the variable will be set to [nil](../commands/nil.md) but will remain listed by [allVariables](../commands/allVariables.md)).

> [!WARNING]
> [missionNamespace](../commands/missionNamespace.md), [uiNamespace](../commands/uiNamespace.md), [parsingNamespace](../commands/parsingNamespace.md) and [profileNamespace](../commands/profileNamespace.md) variables **cannot** be named as commands - e.g `missionNamespace setVariable ["west", 123];` conflicts with the [west](../commands/west.md) command and will result in a `Reserved variable in expression` error, [west](../commands/west.md) being a scripting command (other namespaces do not have such limitation).
> See also all available script commands.

## Syntax

```sqf
varspace setVariable [name, value, public]
```

**Parameters:**

- `varspace`: variable space in which variable can be set. Can be one of:
  - [Namespace](../topics/Namespace.md)
  - [Object](../topics/Object.md)
    - ✓ Arma 3 v2.10 `CfgAmmo` [Object](../topics/Object.md)s (bullets, mines etc)
    - ✗ `CfgNonAIVehicles` [Object](../topics/Object.md)s (butterflies, proxies etc - `EmptyDetector`s (triggers) work)
    - ✗ `1. local` [Object](../topics/Object.md)s (`1. lightpoint`, `1. soundsource` etc - see [createVehicleLocal](../commands/createVehicleLocal.md))
  - [Group](../topics/Group.md)
  - [Location](../topics/Location.md) (Arma 3 v2.10 [scripted ones](../commands/createLocation.md) only)
  - Arma 2 v1.00 [Team Member](../topics/Team_Member.md)
  - Arma 2 v1.00 [Task](../topics/Task.md) - *value* will actually be set on the [Task](../topics/Task.md)'s FSM **if** it exists.
  - Arma 3 v0.50 [Control](../topics/Control.md)
  - Arma 3 v0.50 [Display](../topics/Display.md)
- `name`: [String](../topics/String.md) - variable name ([Identifier](../topics/Identifier.md))
- `value`: [Anything](../topics/Anything.md) - variable value
- `public` - (Optional, default [false](../commands/false.md)) can be one of:
  - [Boolean](../topics/Boolean.md) - if set to [true](../commands/true.md), the variable is broadcast globally and is persistent ([JIP](../topics/Multiplayer_Scripting.md#join-in-progress) compatible) Global Effect
  - [Number](../topics/Number.md) - the variable is only set on the client with the given [Machine network ID](../topics/Multiplayer_Scripting.md#machine-network-id). If the number is negative, the variable is set on every client except for the one with the given ID.
  - [Array](../topics/Array.md) of [Number](../topics/Number.md)s - array of [Machine network IDs](../topics/Multiplayer_Scripting.md#machine-network-id)

  This parameter is only available if the **varspace** parameter is a [Namespace](../topics/Namespace.md), [Object](../topics/Object.md) or [Group](../topics/Group.md). Furthermore, only the following Data Types can be broadcast:

  |  |  |  |  |  |  |  |  |  |  |  |
  |---|---|---|---|---|---|---|---|---|---|---|
  | Type | [Number](../topics/Number.md) | [Boolean](../topics/Boolean.md) | [Object](../topics/Object.md) | [Group](../topics/Group.md) | [String](../topics/String.md) | [Text](../topics/Structured_Text.md) | [Array](../topics/Array.md) | [Code](../topics/Code.md) | [Nothing](../topics/Nothing.md) ([nil](../commands/nil.md)) | [HashMap](../topics/HashMap.md) |
  | Since | Armed Assault v1.00 | Armed Assault v1.09 | Arma 3 v1.26 | Arma 3 v2.02 |  |  |  |  |  |  | *(since arma2 1.00)*

**Returns:** [Nothing](../topics/Nothing.md)

## Examples

### Example 1

```sqf
_myTruck setVariable ["TAG_myPublicVariable", 123, true];
```

### Example 2

```sqf
_myTruck setVariable ["TAG_myLocalVariable", ["321", _var], owner driver _myTruck];
```

### Example 3

```sqf
missionNamespace setVariable ["TAG_myName", "Brian"];
hint TAG_myName; // hints "Brian"
```

### Example 4

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

```sqf
private _var = missionNamespace getVariable "VarName";
if (isNil "_var") then
{
    _var = 123;
    missionNamespace setVariable ["VarName", _var];
};
// _var now contains the current value of the missionNamespace's variable varName
```

### Example 5

Remove `TAG_myPublicVariable` from [missionNamespace](../commands/missionNamespace.md) (globally):

```sqf
missionNamespace setVariable ["TAG_myPublicVariable", nil, true];
```

## See also

[getVariable](../commands/getVariable.md) · [allVariables](../commands/allVariables.md) · [setFSMVariable](../commands/setFSMVariable.md)

## Community notes

**Killswitch** — 2007-08-18

According to Suma, beginning with ArmA version 1.08:

> [setVariable](../commands/setVariable.md) now should work on any entity which can be targeted by AI, including soldier and game logic units. This includes most buildings, but not other static objects.
> **Using it with buildings one should be aware the building may be discarded because of streaming**. In such case the variable space is lost. When used for buildings, the storage should therefore be considered non-reliable.

---

**Commy2** — 2015-12-09

This command does not work with CfgAmmo or CfgNonAIVehicles objects, like bullets, mines or butterflies.

---

**Leopard20** — 2022-03-23

Since Arma 3 v2.10 [setVariable](../commands/setVariable.md) works on CfgAmmo objects (such as bullets, missiles, grenades, mines, etc) as well.

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