{"name":"getVariable","title":"getVariable","type":"command","url":"https://community.bistudio.com/wiki/getVariable","revid":369203,"updated":"2024-09-03T10:34:50Z","groups":["Variables","Namespaces","Multiplayer"],"status":"ok","games":{"arma1":"1.00","arma2":"1.00","arma2oa":"1.50","tkoh":"1.00","arma3":"0.50"},"since":"arma1 1.00","arma3_since":"0.50","locality":{"argument":"global"},"description":"Returns the value of variable in the variable space assigned to various data types.\nAll available data types combinations:\n\n| Primary syntax (String) |  | Alternative syntax (Array) |  |\n|---|---|---|---|\n| **Code** | **Since** | **Code** | **Since** |\n| Namespace getVariable String | - | Namespace getVariable Array | Arma 2: Operation Arrowhead v1.60 |\n| Object getVariable String | - | Object getVariable Array | - |\n| Group getVariable String | - | Group getVariable Array | - |\n| Team Member getVariable String | - | Team Member getVariable Array | - |\n| Task getVariable String | - | Task getVariable Array | Arma 3 v1.68 |\n| Location getVariable String | - | Location getVariable Array | Arma 3 v1.68 |\n| Control getVariable String | - | Control getVariable Array | Arma 3 v1.56 |\n| Display getVariable String | Arma 3 v1.56 | Display getVariable Array | Arma 3 v1.56 |\n\n> [!IMPORTANT]\n> When variable is set on a Task, it is not actually set on the task itself, but on the FSM attached to the task.\n> If there is no FSM, getVariable will not work.\n\n> [!NOTE]\n> **Arma 3:** In the case of Alt Syntax usage in Arma 3, *defaultValue* will be returned if:\n>\n> - Requested variable is either undefined or nil\n> - Variable namespace is null (objNull, grpNull, etc.)","syntaxes":[{"syntax":"varspace getVariable name","params":[{"name":"varspace","type":"Namespace, Object, Display, Control, Group, Location, Task, Team Member, Display, Control","type_expr":"Namespace | Object | Display | Control | Group | Location | Task | Team Member"},{"name":"name","type":"String","description":"variable name in given namespace","type_expr":"String"}],"returns":{"type":"Anything or Nothing","type_expr":"Anything | Nothing","description":"if the variable doesn't exist","text":"Anything or Nothing if the variable doesn't exist"},"form":"binary","left":{"text":"varspace"},"right":{"text":"name"}},{"syntax":"varspace getVariable [name, defaultValue]","params":[{"name":"varspace","type":"Namespace, Object, Display, Control, Group, Location, Task, Team Member, Display, Control","type_expr":"Namespace | Object | Display | Control | Group | Location | Task | Team Member"},{"name":"name","type":"String","description":"variable name in given namespace","type_expr":"String"},{"name":"defaultValue","type":"Anything","description":"value to return if variable doesn't exist\n\n> [!WARNING]\n> A dynamic *defaultValue* e.g `random 500` will always be executed, even if the variable is defined.","type_expr":"Anything"}],"returns":{"type":"Anything","type_expr":"Anything","description":"current value of the variable or defaultValue if the variable doesn't exist.","text":"Anything - current value of the variable or *defaultValue* if the variable doesn't exist."},"form":"binary","left":{"text":"varspace"},"right":{"text":"[name, defaultValue]"}}],"examples":[{"code":["private _variable = myTruck getVariable \"myVariable\"; // returns nil if \"myVariable\" is not set"]},{"code":["private _variable = myTruck getVariable [\"myVariable\", 50]; // returns 50 if \"myVariable\" is not set"]},{"text":"```sqf\nfor \"_i\" from 0 to 5 do\n{\n    _car = missionNamespace getVariable (\"car\" + str _i);\n    _car setDamage 0;\n}; // Set damage of car0..car5 to 0\n```","code":["for \"_i\" from 0 to 5 do\n{\n\t_car = missionNamespace getVariable (\"car\" + str _i);\n\t_car setDamage 0;\n}; // Set damage of car0..car5 to 0"]},{"code":["myMissionVar = 2015;\nmissionNamespace getVariable \"myMissionVar\"; // returns 2015"]},{"text":"Get current value of a variable and if it is undefined, define it and get the defined value:\n\n```sqf\nprivate _var = missionNamespace getVariable \"varName\";\nif (isNil \"_var\") then\n{\n    missionNamespace setVariable [\"varName\", 123];\n    _var = 123;\n};\n// _var here will contain current value of the variable varName\n```","code":["private _var = missionNamespace getVariable \"varName\";\nif (isNil \"_var\") then\n{\n\tmissionNamespace setVariable [\"varName\", 123];\n\t_var = 123;\n};\n// _var here will contain current value of the variable varName"]}],"see_also":["setVariable","allVariables","getFSMVariable"],"notes":[{"user":"Ebay","date":"2016-08-13T17:13:00Z","text":"**NOTE: Arma 2 only! In Arma 3 this will return default value:**\nWarning: the alternative syntax returns undefined when the namespace is an object and that object is null. Example:\n\n```sqf\n_test = objNull getVariable [\"test\", \"0\"];\nsystemChat _test;\n```\n\nErrors because _test is undefined. Tested in Arma 2: Operation Arrowhead 1.63.131129"},{"user":"AgentRev","date":"2017-08-31T03:29:00Z","text":"You can do typechecking using param:\n\n```sqf\nprivate _myVar = [player getVariable \"myVar\"] param [0, \"\", [\"\"]];\n```\n\nand params:\n\n```sqf\n[player getVariable \"myVar\"] params [[\"_myVar\", \"\", [\"\"]]];\n```"}]}