Skip to content

Variables API

Local Variables

Local Name Variables and Local List Variables are components attached to game objects and their value is bound to the scene they are in. To access their runtime values you reference the component and call one of their public methods.

Local Name Variables

Local Name Variables are components attached to game objects and can be referenced like any other script. To access any of its values you can use the following methods:

Getting values

bool Exists(string name)

Returns true if the variable exists. False otherwise

object Get(string name)

Returns the value of the variable. Requires being cast to the correct type

Setting values

void Set(string name, object value)

Sets the value of a variable

Listening to events

You can also register when a Local Name Variable changes using the following methods:

void Register(Action<string> callback)

Executes the callback every time a variable changes its value. The callback receives the name of the variable that changed

void Unregister(Action<string> callback)

Stops executing the callback when the variable changes

Example

The following component keeps track of the score variable of a Local Name Variables component and prints its value every time it changes:

public class PrintScore : MonoBehaviour
{
    public LocalNameVariables variables;

    private void OnEnable()
    {
        this.variables.Register(this.OnChange);
    }

    private void OnDisable()
    {
        this.variables.Unregister(this.OnChange);
    }

    private void OnChange(string name)
    {
        if (name != "score") return;
        Debug.Log("Score: " + this.variables.Get("score"));
    }
}

Local List Variables

A Local List Variables component has the following methods for getting and manipulating its values. Most of them can be called using the index of the element in the list:

Getting values

object Get(int index)

Returns the value found at the index position. If you need to resolve the position using a Pick (for example, the first or a random element of the list), use Get(IListGetPick pick, Args args) instead

int Count

Property that returns the number of elements of the list

bool Contains(object value)

Returns true if the list contains the value. False otherwise

Setting values

void Set(int index, object value)

Sets a value at the index position. It also has a version that takes an IListSetPick pick and Args args instead of the index

void Insert(int index, object value)

Inserts a value at the indexed position. It also has a version that takes an IListGetPick pick and Args args instead of the index

void Push(object value)

Adds a new value at the end of the list

void Remove(int index)

Removes the value found at the index position. It also has a version that takes an IListGetPick pick and Args args instead of the index

void Clear()

Removes all values from the list

void Move(int source, int destination)

Moves the value found at the source position to a new index. It also has a version that takes two IListGetPick parameters and an Args parameter

Listening to events

You can also register when a Local List Variable changes any of its items using the following methods:

void Register(Action<ListVariableRuntime.Change, int> callback)

Executes the callback method whenever there's a change. The callback receives the type of change and the index of the element affected

void Unregister(Action<ListVariableRuntime.Change, int> callback)

Stops executing the callback when the list changes

Global Variables

Global Name Variables and Global List Variables are scriptable objects and their runtime value is stored in a separate singleton manager called GlobalNameVariablesManager and GlobalListVariablesManager.

Example

To read and modify the value of a Global Name Variable called score from a script, reference the Global Name Variables asset and use the manager's singleton instance:

public GlobalNameVariables variables;

public void AddPoints(int points)
{
    GlobalNameVariablesManager manager = GlobalNameVariablesManager.Instance;

    double score = (double) manager.Get(this.variables, "score");
    manager.Set(this.variables, "score", score + points);
}

Note that Number variables are stored as double values.

Global Name Variables

The GlobalNameVariablesManager has the following methods available:

Getting values

bool Exists(GlobalNameVariables asset, string name)

Returns true if the variable exists. False otherwise

object Get(GlobalNameVariables asset, string name)

Returns the value of the variable. Requires being cast to the correct type

Setting values

void Set(GlobalNameVariables asset, string name, object value)

Sets the value of a variable

Listening to events

You can also register when a Global Name Variable changes using the following methods:

void Register(GlobalNameVariables asset, Action<string> callback)

Executes the callback every time the variable changes its value

void Unregister(GlobalNameVariables asset, Action<string> callback)

Stops executing the callback when the variable changes

Global List Variables

The GlobalListVariablesManager has the following methods. Just like the Local List Variables, most of them can also be called using a Pick and an Args parameter instead of the element's index:

Getting values

int Count(GlobalListVariables asset)

Returns the number of elements of the list

object Get(GlobalListVariables asset, int index)

Returns the value found at the index position

bool Contains(GlobalListVariables asset, object value)

Returns true if the list contains the value. False otherwise

Setting values

void Set(GlobalListVariables asset, int index, object value)

Sets a value at the index position

void Insert(GlobalListVariables asset, int index, object value)

Inserts a value at the indexed position

void Push(GlobalListVariables asset, object value)

Adds a new value at the end of the list

void Remove(GlobalListVariables asset, int index)

Removes the value found at the index position

void Clear(GlobalListVariables asset)

Removes all values from the list

void Move(GlobalListVariables asset, int source, int destination)

Moves the value found at the source position to a new index

Listening to events

You can also register when a Global List Variable changes any of its items using the following methods:

void Register(GlobalListVariables asset, Action<ListVariableRuntime.Change, int> callback)

Executes the callback method whenever there's a change

void Unregister(GlobalListVariables asset, Action<ListVariableRuntime.Change, int> callback)

Stops executing the callback when the list changes