{"name":"getVariable","operator":null,"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","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":null,"server_exec":false},"exec":null,"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>\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> - Requested variable is either undefined or nil\n> - Variable namespace is null (objNull, grpNull, etc.)","syntaxes":[{"syntax":"varspace getVariable name","since":null,"effect":null,"exec":null,"params":[{"name":"varspace","type":null,"optional":false,"default":null,"description":"Namespace, Object, Display, Control, Group, Location, Task, Team Member, Display, Control","types":["Namespace","Object","Display","Control","Group","Location","Task","Team Member"],"since":null,"text":"varspace: Namespace, Object, Display, Control, Group, Location, Task, Team Member, Display, Control"},{"name":"name","type":"String","optional":false,"default":null,"description":"variable name in given namespace","types":["String"],"since":null,"text":"name: String - variable name in given namespace"}],"returns":{"type":"Anything or Nothing if the variable doesn't exist","types":["Anything","Nothing"],"description":null,"text":"Anything or Nothing if the variable doesn't exist"},"form":"binary","operator":null,"left":{"text":"varspace","types":["Namespace","Object","Display","Control","Group","Location","Task","Team Member"]},"right":{"text":"name","types":["String"]}},{"syntax":"varspace getVariable [name, defaultValue]","since":null,"effect":null,"exec":null,"params":[{"name":"varspace","type":null,"optional":false,"default":null,"description":"Namespace, Object, Display, Control, Group, Location, Task, Team Member, Display, Control","types":["Namespace","Object","Display","Control","Group","Location","Task","Team Member"],"since":null,"text":"varspace: Namespace, Object, Display, Control, Group, Location, Task, Team Member, Display, Control"},{"name":"name","type":"String","optional":false,"default":null,"description":"variable name in given namespace","types":["String"],"since":null,"text":"name: String - variable name in given namespace"},{"name":"defaultValue","type":"Anything","optional":false,"default":null,"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.","types":["Anything"],"since":null,"text":"defaultValue: Anything - 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."}],"returns":{"type":"Anything","types":["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","operator":null,"left":{"text":"varspace","types":["Namespace","Object","Display","Control","Group","Location","Task","Team Member"]},"right":{"text":"[name, defaultValue]","types":["Array"]}}],"examples":[{"text":"```sqf\nprivate _variable = myTruck getVariable \"myVariable\"; // returns nil if \"myVariable\" is not set\n```","code":["private _variable = myTruck getVariable \"myVariable\"; // returns nil if \"myVariable\" is not set"]},{"text":"```sqf\nprivate _variable = myTruck getVariable [\"myVariable\", 50]; // returns 50 if \"myVariable\" is not set\n```","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"]},{"text":"```sqf\nmyMissionVar = 2015;\nmissionNamespace getVariable \"myMissionVar\"; // returns 2015\n```","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"]}],"multiplayer":null,"problems":null,"aliases":[],"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```"}],"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","Take On Helicopters: Scripting Commands"]}