---
name: "addPublicVariableEventHandler"
type: command
status: broken
replaced_by: ["remoteExec", "remoteExecCall"]
groups: ["Broken Commands", "Event Handlers", "Multiplayer", "Variables"]
arma3_since: "0.50"
locality: {"argument": "global", "effect": "local", "server_exec": false}
source: https://community.bistudio.com/wiki/addPublicVariableEventHandler
revid: 376526
updated: 2025-10-24T10:02:40Z
---

# addPublicVariableEventHandler

*Command · Broken Commands, Event Handlers, Multiplayer, Variables · Arma 3 v0.50 · introduced in Armed Assault v1.09*

> [!WARNING]
> This command is listed as broken on the wiki. Use [remoteExec](../commands/remoteExec.md) or [remoteExecCall](../commands/remoteExecCall.md) instead.

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

## Description

> [!NOTE]
> This command is deprecated since Arma 3 v1.52 and the introduction of [remoteExec](../commands/remoteExec.md) and [remoteExecCall](../commands/remoteExecCall.md).

This event handler will detect if a [missionNamespace](../commands/missionNamespace.md) variable (it is attached to) has been broadcast over network with [publicVariable](../commands/publicVariable.md), [publicVariableClient](../commands/publicVariableClient.md) or [publicVariableServer](../commands/publicVariableServer.md) commands and will execute EH code upon detection.

> [!NOTE]
> - This EH works **only** in Multiplayer environment
> - There is **no way** to remove this EH
> - This EH will not fire on the machine that executed broadcast with [publicVariable](../commands/publicVariable.md) command, only on the machines that receive the broadcast
> - The value of broadcast variable can be exactly the same, it is the actual broadcast that triggers EH not the change in variable

## Syntax

### Syntax 1

```sqf
varName addPublicVariableEventHandler code
```

**Parameters:**

- `varName`: [String](../topics/String.md) - [missionNamespace](../commands/missionNamespace.md) variable name
- `code`: [Code](../topics/Code.md) - by default, the code is executed in [missionNamespace](../commands/missionNamespace.md). `_this` arguments array is in format [varName, value, target]:
  - varName: [String](../topics/String.md) - broadcast variable name (same variable name EH is attached to)
  - value: [Anything](../topics/Anything.md) - broadcast variable value
  - target: [Object](../topics/Object.md) or [Group](../topics/Group.md) - target the variable got set on with [setVariable](../commands/setVariable.md) (see [#Alternative Syntax](../commands/addPublicVariableEventHandler.md#alternative-syntax))

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

### Syntax 2

*since arma3 1.46*

```sqf
varName addPublicVariableEventHandler [target, code]
```

**Parameters:**

- `varName`: [String](../topics/String.md) - name of the variable set on target with [setVariable](../commands/setVariable.md)
- `target`: [Object](../topics/Object.md), [Group](../topics/Group.md) or [Team Member](../topics/Team_Member.md)
- `code`: [Code](../topics/Code.md)

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

## Examples

### Example 1

```sqf
"publicThis" addPublicVariableEventHandler {
    _varName = _this select 0;
    _value = _this select 1;
    _target = _this select 2;
    hint format ["%1 has been updated to %2 for %3", _varName, _value, target];
};
```

### Example 2

Client:

```sqf
"'^:)123BURP,+=lol" addPublicVariableEventHandler {hint ("NUTS are " + (_this select 1))};
```

Server:

```sqf
missionNamespace setVariable ["'^:)123BURP,+=lol", "craZZZZy"];
publicVariable "'^:)123BURP,+=lol";
```

## Problems

> [!WARNING]
> The [#Alternative Syntax](../commands/addPublicVariableEventHandler.md#alternative-syntax) does not work as intended.

## See also

[publicVariable](../commands/publicVariable.md) · [publicVariableClient](../commands/publicVariableClient.md) · [publicVariableServer](../commands/publicVariableServer.md)

## Community notes

**TeRp** — 2007-12-22

Please note that varName indicates which variable you want to monitor with this eventhandler.

As a result, the example eventhandler on this page will only fire when the variable publicThis has been changed,
but not if any other variable was changed by any other client via the publicVariable command.

---

**MulleDK13** — 2014-02-27

Note on using addPublicVariableEventHandler during initialization: If you need a function to call addPublicVariableEventHandler during initialization, you must use postInit. addPublicVariableEventHandler does not work during preInit.

---

**Killzone_Kid** — 2014-02-27

MulleDK13's note above needs some clarification. You don't "must" use postInit and you absolutely can use preInit function to initialise [addPublicVariableEventHandler](../commands/addPublicVariableEventHandler.md) if you start [scheduled](../topics/Scheduler.md#scheduled_environment) script from it.

```sqf
// script with preInit = 1; in CfgFunctions
0 spawn {
    "someVar" addPublicVariableEventHandler {
        // yourcode
    };
};
```

---

**SilentSpike** — 2015-11-23

While it is true that the event handler will only fire on the machine receiving the broadcast value, please note that this machine can actually be the same machine broadcasting it in the cases of [publicVariableClient](../commands/publicVariableClient.md) and [publicVariableServer](../commands/publicVariableServer.md).

Examples:

```sqf
if (isServer) then
{
    "OnServer" addPublicVariableEventHandler { hint "This event handler still fired!"; };
    publicVariableServer "OnServer";
};
```

```sqf
// This example assumes the client knows their own client ID
// It does also work on the server (when the server ID is used) irrespective of the command name

"OnClient" addPublicVariableEventHandler { hint "This event handler still fired!"; };
clientID publicVariableClient "OnClient";
```

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