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);

Scopes

Graph

To access variables on a graph:

Variables.Graph(flowGraph)

Or:

Variables.Graph(flowMachine)

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"))
{
    // ...
}

This article was helpful for 8 people. Is this article helpful for you?

This API change in 1.4?  I had some code using this without any problems, but post 1.4 it is no longer happy about it.