Note |
---|
To use Bolt, which is Unity’s visual scripting solution, you must purchase it on the Unity Asset Store. |
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);
Make sure you add the following usings to your C# script to access the API:
using Ludiq;
using Bolt;
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)
To access variables on an object:
Variables.Object(gameObject)
To access sceneA Scene contains the environments and menus of your game. Think of each unique Scene file as a unique level. In each Scene, you place your environments, obstacles, and decorations, essentially designing and building your game in pieces. More info
See in Glossary variables:
Variables.Scene(scene)
Or:
Variables.Scene(gameObjectInScene)
Or:
Variables.ActiveScene
To access application variables:
Variables.Application
To access saved variables:
Variables.Saved
In these examples, the lowercase scope refers to one of the scopes above.
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")
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.
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"))
{
// ...
}