A game about forced loneliness, made by TACStudios
1# Variables node 2 3> [!NOTE] 4> For versions 2019/2020 LTS, download the Visual Scripting package from the [Unity Asset Store](https://assetstore.unity.com/packages/tools/visual-bolt-163802). 5 6There are six kinds of [variable](vs-variables.md) nodes. Each of these variable nodes has three object nodes: 7 8* Get, to retrieve the value of the variable 9* Set, to assign a new value to the variable 10* Is Defined, to check whether the variable is defined 11 12They are located under the Variables category in the fuzzy finder. 13 14 15Variable nodes are teal colored. 16 17## Dynamic Typing 18 19For get / set nodes, variables are not statically typed, meaning their type can change at runtime. Their type displays as an object when defined from the blackboard window. 20 21### Get Variable 22![](images/vs-variables-get-variable-node.png) 23 24The get variable node requires the name of the variable as an input and returns the **Value** as an output. 25 26### Set Variable 27![](images/vs-variables-set-variable-node.png) 28 29 30The set variable nodes require the name of the variable and the new value assigned to it as inputs. For convenience in layouting, it returns this same value as an output. 31 32Connect the control input port to indicate when the variable should be assigned and, optionally, the control output port to indicate what to do after. 33 34Using a set node with a variable name that doesn't yet exist creates the variable. 35 36### Has Variable 37 38The Has Variable nodes require the name of the variable as an input and returns an Is Defined boolean as an output. They're useful to check if a variable has been created, and often, provide a fallback value if it hasn't. 39 40![](images/vs-variables-has-variable-node.png) 41 42 43Do the same thing more easily by checking the Fallback box in the graph inspector for a Get Variable node. This adds a Fallback input to the node that is returned if the variable hasn't been defined: 44 45 46![](images/vs-variables-get-variable-fallback-option.png) 47 48## Dynamic Variables 49 50As the name of the variable is a standard value input port, connect it to any other port that returns a string. Refer to "dynamic variables", that is, variables whose reference might change during play mode. 51 52## Object Variables 53 54Object variable nodes require an additional input for the Source. That port indicates which game object the variable you're referring to is defined. When left to its default value, they look on the current object (self). 55 56For example, the Get Variable node gets the value of the health variable on the player2 object. 57 58![](images/vs-variables-get-object-variable-object.png) 59 60## Dropdowns 61 62The kind and the name dropdowns can quickly configure the variable nodes. The name suggestions are contextual and are based on the existing variables of this kind and on the other variable nodes in the current graph. 63 64![](images/vs-variables-change-scope-node.png) 65 66## Drag and Drop 67 68Drag and drop items from the blackboard window directly into the graph to create matching nodes. 69 70* By default, a Get node is created. 71* If the Alt key is held, a Set node is created. 72* If the Shiftkey is held, an Is Defined node is created. 73 74## Variables API 75 76Visual scripting provides an easy API to handle variables, to get or set their value and verify if they are defined. All these operations are available from the Variables class. 77 78For example: 79 80``` 81Variables.Application.Set("score", 100); 82``` 83 84*** 85 86### Usings 87 88Add the following usings to your C# script to access the API: 89 90``` 91using Unity.VisualScripting; 92``` 93 94*** 95 96## Scope 97 98### Graph 99 100To access variables on a graph, create a graph reference. This is basically a path to the nested graph from its root machine. 101 102To get the root graph on a machine: 103 104``` 105var graphReference = GraphReference.New(flowMachine, true); 106``` 107 108To access nested graphs, pass their parent nodes as additional parameters: 109 110``` 111var graphReference = GraphReference.New(flowMachine, new IGraphParentElement[] { superUnit }, true); 112``` 113 114To pass a graph reference: 115 116``` 117Variables.Graph(graphReference) 118``` 119 120### Object 121 122To access variables on an object: 123 124``` 125Variables.Object(gameObject) 126``` 127 128### Scene 129 130To access scene variables, do one of the following: 131 132``` 133Variables.Scene(scene) 134``` 135 136Or: 137 138``` 139Variables.Scene(gameObjectInScene) 140``` 141 142Or: 143 144``` 145Variables.ActiveScene 146``` 147 148### Application 149 150To access application variables: 151 152``` 153Variables.Application 154``` 155 156### Saved 157 158To access saved variables: 159 160``` 161Variables.Saved 162``` 163 164*** 165 166## Operations 167 168In these examples, the lowercase scope refers to one of the previous scopes. 169 170### Get 171 172To get the value of a variable, use the Get method with a name parameter: 173 174``` 175scope.Get("name"); 176``` 177 178Note that variables are not strongly typed; they need to be cast manually. For example: 179 180``` 181int health = (int)Variables.Object(player).Get("health") 182``` 183 184### Set 185 186To set the value of a variable, use the Set method with the name and value parameters: 187 188``` 189scope.Set("name", value); 190``` 191 192For example: 193 194``` 195Variables.Object(player).Set("health", 100); 196``` 197 198Because variables are not strongly typed, pass any value to the second parameter, even if the variable currently is of a different type. 199 200> [!NOTE] 201> Using the set method with a variable name that does not yet exist defines a new variable. 202 203### Is Defined 204 205To check if a variable is defined, use the IsDefined method with a name parameter: 206 207``` 208scope.IsDefined("name"); 209``` 210 211For example: 212 213``` 214if (Variables.Application.IsDefined("score")) 215{ 216 // ... 217} 218``` 219