{"name":"setVariable","operator":null,"title":"setVariable","type":"command","url":"https://community.bistudio.com/wiki/setVariable","revid":369355,"updated":"2024-09-03T18:43:04Z","groups":["Variables","Namespaces","Multiplayer"],"status":"ok","replaced_by":[],"games":[{"game":"arma1","name":"Armed Assault","version":"1.00"},{"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.00"},"arma3_since":"0.50","branch":null,"locality":{"argument":"global","effect":"local","server_exec":false},"exec":null,"description":"Set variable to given value in the variable space of given element. Can be used to broadcast variables over the network.\n\nTo remove a variable, set it to nil (see #Example 5) - note that this does not work on Object and scripted Location namespaces (the variable will be set to nil but will remain listed by allVariables).\n\n> [!WARNING]\n> missionNamespace, uiNamespace, parsingNamespace and profileNamespace variables **cannot** be named as commands - e.g `missionNamespace setVariable [\"west\", 123];` conflicts with the west command and will result in a `Reserved variable in expression` error, west being a scripting command (other namespaces do not have such limitation).\n> See also all available script commands.","syntaxes":[{"syntax":"varspace setVariable [name, value, public]","since":null,"effect":null,"exec":null,"params":[{"name":"varspace","type":null,"optional":false,"default":null,"description":"variable space in which variable can be set. Can be one of:\n- Namespace\n- Object\n  - ✓ Arma 3 v2.10 `CfgAmmo` Objects (bullets, mines etc)\n  - ✗ `CfgNonAIVehicles` Objects (butterflies, proxies etc - `EmptyDetector`s (triggers) work)\n  - ✗ `1. local` Objects (`1. lightpoint`, `1. soundsource` etc - see createVehicleLocal)\n- Group\n- Location (Arma 3 v2.10 scripted ones only)\n- Arma 2 v1.00 Team Member\n- Arma 2 v1.00 Task - *value* will actually be set on the Task's FSM **if** it exists.\n- Arma 3 v0.50 Control\n- Arma 3 v0.50 Display","types":[],"since":null,"text":"varspace: variable space in which variable can be set. Can be one of:\n- Namespace\n- Object\n  - ✓ Arma 3 v2.10 `CfgAmmo` Objects (bullets, mines etc)\n  - ✗ `CfgNonAIVehicles` Objects (butterflies, proxies etc - `EmptyDetector`s (triggers) work)\n  - ✗ `1. local` Objects (`1. lightpoint`, `1. soundsource` etc - see createVehicleLocal)\n- Group\n- Location (Arma 3 v2.10 scripted ones only)\n- Arma 2 v1.00 Team Member\n- Arma 2 v1.00 Task - *value* will actually be set on the Task's FSM **if** it exists.\n- Arma 3 v0.50 Control\n- Arma 3 v0.50 Display"},{"name":"name","type":"String","optional":false,"default":null,"description":"variable name (Identifier)","types":["String"],"since":null,"text":"name: String - variable name (Identifier)"},{"name":"value","type":"Anything","optional":false,"default":null,"description":"variable value","types":["Anything"],"since":null,"text":"value: Anything - variable value"},{"name":"public","type":null,"optional":true,"default":"false","description":"can be one of:\n- Boolean - if set to true, the variable is broadcast globally and is persistent (JIP compatible) Global Effect\n- Number - the variable is only set on the client with the given Machine network ID. If the number is negative, the variable is set on every client except for the one with the given ID.\n- Array of Numbers - array of Machine network IDs\n\nThis parameter is only available if the **varspace** parameter is a Namespace, Object or Group. Furthermore, only the following Data Types can be broadcast:\n\n|  |  |  |  |  |  |  |  |  |  |  |\n|---|---|---|---|---|---|---|---|---|---|---|\n| Type | Number | Boolean | Object | Group | String | Text | Array | Code | Nothing (nil) | HashMap |\n| Since | Armed Assault v1.00 | Armed Assault v1.09 | Arma 3 v1.26 | Arma 3 v2.02 |  |  |  |  |  |  |","types":[],"since":"arma2 1.00","text":"public - (Optional, default false) can be one of:\n- Boolean - if set to true, the variable is broadcast globally and is persistent (JIP compatible) Global Effect\n- Number - the variable is only set on the client with the given Machine network ID. If the number is negative, the variable is set on every client except for the one with the given ID.\n- Array of Numbers - array of Machine network IDs\n\nThis parameter is only available if the **varspace** parameter is a Namespace, Object or Group. Furthermore, only the following Data Types can be broadcast:\n\n|  |  |  |  |  |  |  |  |  |  |  |\n|---|---|---|---|---|---|---|---|---|---|---|\n| Type | Number | Boolean | Object | Group | String | Text | Array | Code | Nothing (nil) | HashMap |\n| Since | Armed Assault v1.00 | Armed Assault v1.09 | Arma 3 v1.26 | Arma 3 v2.02 |  |  |  |  |  |  |"}],"returns":{"type":"Nothing","types":["Nothing"],"description":null,"text":"Nothing"},"form":"binary","operator":null,"left":{"text":"varspace","types":[]},"right":{"text":"[name, value, public]","types":["Array"]}}],"examples":[{"text":"```sqf\n_myTruck setVariable [\"TAG_myPublicVariable\", 123, true];\n```","code":["_myTruck setVariable [\"TAG_myPublicVariable\", 123, true];"]},{"text":"```sqf\n_myTruck setVariable [\"TAG_myLocalVariable\", [\"321\", _var], owner driver _myTruck];\n```","code":["_myTruck setVariable [\"TAG_myLocalVariable\", [\"321\", _var], owner driver _myTruck];"]},{"text":"```sqf\nmissionNamespace setVariable [\"TAG_myName\", \"Brian\"];\nhint TAG_myName; // hints \"Brian\"\n```","code":["missionNamespace setVariable [\"TAG_myName\", \"Brian\"];\nhint TAG_myName; // hints \"Brian\""]},{"text":"Get the current value of a variable or, if it is undefined, define it and then get the value:\n\n```sqf\nprivate _var = missionNamespace getVariable \"VarName\";\nif (isNil \"_var\") then\n{\n    _var = 123;\n    missionNamespace setVariable [\"VarName\", _var];\n};\n// _var now contains the current value of the missionNamespace's variable varName\n```","code":["private _var = missionNamespace getVariable \"VarName\";\nif (isNil \"_var\") then\n{\n\t_var = 123;\n\tmissionNamespace setVariable [\"VarName\", _var];\n};\n// _var now contains the current value of the missionNamespace's variable varName"]},{"text":"Remove `TAG_myPublicVariable` from missionNamespace (globally):\n\n```sqf\nmissionNamespace setVariable [\"TAG_myPublicVariable\", nil, true];\n```","code":["missionNamespace setVariable [\"TAG_myPublicVariable\", nil, true];"]}],"multiplayer":null,"problems":null,"aliases":[],"see_also":["getVariable","allVariables","setFSMVariable"],"notes":[{"user":"Killswitch","date":"2007-08-18T09:51:00Z","text":"According to Suma, beginning with ArmA version 1.08:\n\n> setVariable 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.\n> **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."},{"user":"Commy2","date":"2015-12-09T20:18:00Z","text":"This command does not work with CfgAmmo or CfgNonAIVehicles objects, like bullets, mines or butterflies."},{"user":"Leopard20","date":"2022-03-23T08:59:50Z","text":"Since Arma 3 v2.10 setVariable works on CfgAmmo objects (such as bullets, missiles, grenades, mines, etc) as well."}],"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: Multiplayer","Command Group: Namespaces","Command Group: Variables","Introduced with Armed Assault version 1.00","Scripting Commands","Scripting Commands: Local Effect","Take On Helicopters: Scripting Commands"]}