{"name":"addPublicVariableEventHandler","operator":null,"title":"addPublicVariableEventHandler","type":"command","url":"https://community.bistudio.com/wiki/addPublicVariableEventHandler","revid":376526,"updated":"2025-10-24T10:02:40Z","groups":["Broken Commands","Event Handlers","Multiplayer","Variables"],"status":"broken","replaced_by":["remoteExec","remoteExecCall"],"games":[{"game":"arma1","name":"Armed Assault","version":"1.09"},{"game":"arma2","name":"Arma 2","version":"1.00"},{"game":"arma2oa","name":"Arma 2: Operation Arrowhead","version":"1.50"},{"game":"tkoh","name":"Take On Helicopters","version":"1.00"},{"game":"arma3","name":"Arma 3","version":"0.50"}],"since":{"game":"arma1","name":"Armed Assault","version":"1.09"},"arma3_since":"0.50","branch":null,"locality":{"argument":"global","effect":"local","server_exec":false},"exec":null,"description":"> [!NOTE]\n> This command is deprecated since Arma 3 v1.52 and the introduction of remoteExec and remoteExecCall.\n\nThis 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.\n\n> [!NOTE]\n> - This EH works **only** in Multiplayer environment\n> - There is **no way** to remove this EH\n> - This EH will not fire on the machine that executed broadcast with publicVariable command, only on the machines that receive the broadcast\n> - The value of broadcast variable can be exactly the same, it is the actual broadcast that triggers EH not the change in variable","syntaxes":[{"syntax":"varName addPublicVariableEventHandler code","since":null,"effect":null,"exec":null,"params":[{"name":"varName","type":"String","optional":false,"default":null,"description":"missionNamespace variable name","types":["String"],"since":null,"text":"varName: String - missionNamespace variable name"},{"name":"code","type":"Code","optional":false,"default":null,"description":"by default, the code is executed in missionNamespace. `_this` arguments array is in format [varName, value, target]:\n- varName: String - broadcast variable name (same variable name EH is attached to)\n- value: Anything - broadcast variable value\n- target: Object or Group - target the variable got set on with setVariable (see #Alternative Syntax)","types":["Code"],"since":null,"text":"code: Code - by default, the code is executed in missionNamespace. `_this` arguments array is in format [varName, value, target]:\n- varName: String - broadcast variable name (same variable name EH is attached to)\n- value: Anything - broadcast variable value\n- target: Object or Group - target the variable got set on with setVariable (see #Alternative Syntax)"}],"returns":{"type":"Nothing","types":["Nothing"],"description":null,"text":"Nothing"},"form":"binary","operator":null,"left":{"text":"varName","types":["String"]},"right":{"text":"code","types":["Code"]}},{"syntax":"varName addPublicVariableEventHandler [target, code]","since":"arma3 1.46","effect":null,"exec":null,"params":[{"name":"varName","type":"String","optional":false,"default":null,"description":"name of the variable set on target with setVariable","types":["String"],"since":null,"text":"varName: String - name of the variable set on target with setVariable"},{"name":"target","type":"Object, Group or Team Member","optional":false,"default":null,"description":null,"types":["Object","Group","Team Member"],"since":null,"text":"target: Object, Group or Team Member"},{"name":"code","type":"Code","optional":false,"default":null,"description":null,"types":["Code"],"since":null,"text":"code: Code"}],"returns":{"type":"Nothing","types":["Nothing"],"description":null,"text":"Nothing"},"form":"binary","operator":null,"left":{"text":"varName","types":["String"]},"right":{"text":"[target, code]","types":["Array"]}}],"examples":[{"text":"```sqf\n\"publicThis\" addPublicVariableEventHandler {\n    _varName = _this select 0;\n    _value = _this select 1;\n    _target = _this select 2;\n    hint format [\"%1 has been updated to %2 for %3\", _varName, _value, target];\n};\n```","code":["\"publicThis\" addPublicVariableEventHandler {\n\t_varName = _this select 0;\n\t_value = _this select 1;\n\t_target = _this select 2;\n\thint format [\"%1 has been updated to %2 for %3\", _varName, _value, target];\n};"]},{"text":"Client:\n\n```sqf\n\"'^:)123BURP,+=lol\" addPublicVariableEventHandler {hint (\"NUTS are \" + (_this select 1))};\n```\n\nServer:\n\n```sqf\nmissionNamespace setVariable [\"'^:)123BURP,+=lol\", \"craZZZZy\"];\npublicVariable \"'^:)123BURP,+=lol\";\n```","code":["\"'^:)123BURP,+=lol\" addPublicVariableEventHandler {hint (\"NUTS are \" + (_this select 1))};","missionNamespace setVariable [\"'^:)123BURP,+=lol\", \"craZZZZy\"];\npublicVariable \"'^:)123BURP,+=lol\";"]}],"multiplayer":null,"problems":"> [!WARNING]\n> The #Alternative Syntax does not work as intended.","aliases":[],"see_also":["publicVariable","publicVariableClient","publicVariableServer"],"notes":[{"user":"TeRp","date":"2007-12-22T13:30:00Z","text":"Please note that varName indicates which variable you want to monitor with this eventhandler.\n\nAs a result, the example eventhandler on this page will only fire when the variable publicThis has been changed,\nbut not if any other variable was changed by any other client via the publicVariable command."},{"user":"MulleDK13","date":"2014-02-27T00:57:00Z","text":"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."},{"user":"Killzone_Kid","date":"2014-02-27T08:40:00Z","text":"MulleDK13's note above needs some clarification. You don't \"must\" use postInit and you absolutely can use preInit function to initialise addPublicVariableEventHandler if you start scheduled script from it.\n\n```sqf\n// script with preInit = 1; in CfgFunctions\n0 spawn {\n    \"someVar\" addPublicVariableEventHandler {\n        // yourcode\n    };\n};\n```"},{"user":"SilentSpike","date":"2015-11-23T21:36:00Z","text":"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.\n\nExamples:\n\n```sqf\nif (isServer) then\n{\n    \"OnServer\" addPublicVariableEventHandler { hint \"This event handler still fired!\"; };\n    publicVariableServer \"OnServer\";\n};\n```\n\n```sqf\n// This example assumes the client knows their own client ID\n// It does also work on the server (when the server ID is used) irrespective of the command name\n\n\"OnClient\" addPublicVariableEventHandler { hint \"This event handler still fired!\"; };\nclientID publicVariableClient \"OnClient\";\n```"}],"categories":["ArmA: Armed Assault: New Scripting Commands","ArmA: Armed Assault: Scripting Commands","Arma 2: Operation Arrowhead: Scripting Commands","Arma 2: Scripting Commands","Arma 3: Scripting Commands","Command Group: Broken Commands","Command Group: Event Handlers","Command Group: Multiplayer","Command Group: Variables","Introduced with Armed Assault version 1.09","Scripting Commands","Scripting Commands: Local Effect","Take On Helicopters: Scripting Commands"]}