Variables API
Bolt provides an easy API to handle variables, allowing to get or set their value and check if they are defined. All these operations are available from the Variables class.
For example:
Variables.Application.Set("score", 100);
Usings
Make sure you add the following usings to your C# script to access the API:
using Ludiq;
using Bolt;
Scopes
Graph
To access variables on a graph, you first need to create a graph reference. This is basically a path to the nested graph from its root machine.
If you simply want to get the root graph on a machine, you can use:
var graphReference = GraphReference.New(flowMachine, true);
To access nested graphs, you will need to pass their parent nodes as additional parameters, for example:
var graphReference = GraphReference.New(flowMachine, new IGraphParentElement[] { superUnit }, true);
Finally, just pass your graph reference:
Variables.Graph(graphReference)
Object
To access variables on an object:
Variables.Object(gameObject)
Scene
To access scene variables:
Variables.Scene(scene)
Or:
Variables.Scene(gameObjectInScene)
Or:
Variables.ActiveScene
Application
To access application variables:
Variables.Application
Saved
To access saved variables:
Variables.Saved
Operations
In these examples, the lowercase scope refers to one of the scopes above.
Get
To get the value of a variable, use the Get method with a name parameter:
scope.Get("name");
Note that variables are not strongly typed, so you will need to cast them manually. For example:
int health = (int)Variables.Object(player).Get("health")
Set
To set the value of a variable, use the Set method with name and value parameters:
scope.Set("name", value);
For example:
Variables.Object(player).Set("health", 100);
Because variables are not strongly typed, you can pass any value to the second parameter, even if the variable currently is of a different type.
Using the set method with a variable name that does not yet exist will define a new variable.
Is Defined
To check if a variable is defined, use the IsDefined method with a name parameter:
scope.IsDefined("name");
For example:
if (Variables.Application.IsDefined("score"))
{
// ...
}