{"name":"publicVariable","title":"publicVariable","type":"command","url":"https://community.bistudio.com/wiki/publicVariable","revid":343848,"updated":"2023-07-26T14:57:01Z","groups":["Multiplayer","Variables"],"status":"ok","games":{"ofp":"1.34","ofpe":"1.00","arma1":"1.00","arma2":"1.00","arma2oa":"1.50","tkoh":"1.00","arma3":"0.50"},"since":"ofp 1.34","arma3_since":"0.50","locality":{"argument":"local","effect":"global"},"description":"Reliably broadcasts missionNamespace variable and its **current** value to all computers (server/client).\nVariables broadcast with publicVariable during a mission will be available to JIP clients with the value they held at the time.\nSuch variables are persistent and sent to the JIP client before the first batch of client-side Event Scripts (such as init.sqf) is run.\n\n> [!IMPORTANT]\n> Using publicVariable too frequently and/or with a lot of data can cause other aspects of the game to experience bandwidth problems.\n\nThe following Data Types are supported:\n\n|  |  |  |  |  |  |  |  |  |  |  |\n|---|---|---|---|---|---|---|---|---|---|---|\n| Type | Number | Boolean | Object | Group | String | Text | Array | Code | Nothing (nil) | HashMap |\n| Since | Operation Flashpoint v1.34 |  |  |  | Armed Assault v1.00 |  | Armed Assault v1.09 |  | Arma 3 v1.26 | Arma 3 v2.02 |\n\n> [!IMPORTANT]\n> It is not possible (and illogical) to transfer a local entity reference, such as scripts, displays or local objects.\n> Also, note that Team Member is not supported.","syntaxes":[{"syntax":"publicVariable varName","params":[{"name":"varName","type":"String","description":"the global variable's Identifier","type_expr":"String"}],"returns":{"type":"Nothing","type_expr":"Nothing"},"form":"unary","right":{"text":"varName"}}],"examples":[{"text":"```sqf\nTAG_MyPublicVariable = 0;\nTAG_MyPublicVariable = 1;\npublicVariable \"TAG_MyPublicVariable\";    // other clients will receive the \"TAG_MyPublicVariable\" variable with a 1 value\n\nTAG_MyPublicVariable = 2;                // needs to be broadcast again - synchronisation is not automatic\n```","code":["TAG_MyPublicVariable = 0;\nTAG_MyPublicVariable = 1;\npublicVariable \"TAG_MyPublicVariable\";\t// other clients will receive the \"TAG_MyPublicVariable\" variable with a 1 value\n\nTAG_MyPublicVariable = 2;\t\t\t\t// needs to be broadcast again - synchronisation is not automatic"]},{"text":"JIP example:\n\n```sqf\nif (isNil \"TAG_CurrentTarget\") then    // has the variable already been set and broadcast?\n{\n    TAG_CurrentTarget = objNull;    // if not, set it on the local machine\n};\n\nplayer doTarget TAG_CurrentTarget;\n```","code":["if (isNil \"TAG_CurrentTarget\") then\t// has the variable already been set and broadcast?\n{\n\tTAG_CurrentTarget = objNull;\t// if not, set it on the local machine\n};\n\nplayer doTarget TAG_CurrentTarget;"]},{"text":"```sqf\nTAG_BossName = \"EvilBigBoss\";\n\npublicVariable TAG_BossName;    // wrong - will try to publicVariable \"EvilBigBoss\" variable, that does not exist\npublicVariable \"TAG_BossName\";    // correct - important, do not forget the QUOTES\n```","code":["TAG_BossName = \"EvilBigBoss\";\n\npublicVariable TAG_BossName;\t// wrong - will try to publicVariable \"EvilBigBoss\" variable, that does not exist\npublicVariable \"TAG_BossName\";\t// correct - important, do not forget the QUOTES"]}],"see_also":["Multiplayer Scripting","Initialisation Order","addPublicVariableEventHandler","publicVariableClient","publicVariableServer","Variables","Event Scripts"]}