Arma 3 Scripting

Command in Broken Commands, Event Handlers, Multiplayer, Variables

addPublicVariableEventHandler

Also available as Markdown and JSON, or read the original wiki page.

Warning

This command is listed as broken on the wiki. Use remoteExec or remoteExecCall instead.

Description

Note

This command is deprecated since Arma 3 v1.52 and the introduction of remoteExec and remoteExecCall.

This event handler will detect if a missionNamespace variable (it is attached to) has been broadcast over network with publicVariable, publicVariableClient or publicVariableServer 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 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

varName addPublicVariableEventHandler code

Parameters:

Returns: Nothing

Syntax 2

since arma3 1.46

varName addPublicVariableEventHandler [target, code]

Parameters:

Returns: Nothing

Examples

Example 1

"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:

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

Server:

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

Problems

Warning

The #Alternative Syntax does not work as intended.

See also

publicVariable · publicVariableClient · publicVariableServer

Community notes

TeRp

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

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.

SilentSpike

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 and publicVariableServer.

Examples:

if (isServer) then
{
    "OnServer" addPublicVariableEventHandler { hint "This event handler still fired!"; };
    publicVariableServer "OnServer";
};
// 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";