Skip to content

Variables

CyanLaser edited this page Jun 29, 2022 · 3 revisions

Variables Variables are containers for data that you can access. You can check or use data in a variable and put data into a variable. Variables were not a direct feature in SDK2, but are common in Udon and other programming languages.

Reason for variables

Why would you use a variable? The main reason to use variables is to reuse or modify data. More intermediate logic using CyanTriggers will require using variables. As an example, in order to toggle an object on or off based on its current state, you will need a variable. Here are the steps:

  • Define a bool variable
  • Get the active state of a GameObject and store it in the variable
  • Flip the bool value (Unary Negation)
  • Set the active state of the GameObject to the value of the variable.

ToggleActive

In this example, a variable was used to read a value, manipulate the value and then set the state using this manipulated value. There are many other example usages of variables. It is best to understand them to be able to create more advanced logic using CyanTriggers. See some of the Guides for more examples using variables.

SDK2 and Variables

In SDK2, all references to data were constant in the inspector. You could not change them at runtime. If you wanted to work with two different values, you needed to make two different versions of the Event that worked with either value. The closest thing to a variable that could be used was the Animator Parameter. VRC_Triggers had the ability to modify Animator Parameters, but never read them back. Animators could use the parameters, but not change them. This mismatch between reading and modifying made it difficult to create more advanced logic in SDK2.

Local vs Global

CyanTriggers have two different types of variables: Global variables and Local variables.

Global Variable

Global variables can be used in any action in the entire CyanTrigger. These variables are defined at the top of the CyanTrigger interface. Any change to a global variable will see the results in every action that uses it. Global variables are the only variable types that can be synced and have OnVariableChanged call back events.

Local Variable

In CyanTriggers, you can use the Variable action to define a local variable of a specific type. Local variables can only be accessed within the Block they have been defined in. When defined, they will always be initialized to the value set. This means that local variables cannot be used to store data between different executions of an action.

OnVariableChanged

The OnVariableChanged event is an event that will be called when a global variable’s value has changed, either through an action setting its value, a remote UdonBehaviour or CyanTrigger used SetProgramVariable on this variable, or the value changed through network sync. This action will be called immediately on change of the variable. Inside the OnVariableChanged event, you have access to the previous or old value to see what it used to be.

OnvariableChanged

Note, do not set the value of the variable within the OnVariableChanged event, as this will cause an infinite loop.

If you are using OnVariableChanged to handle synced variable updates, be careful to not depend on any other synced variable’s value. Synced variables are updated in an arbitrary order. If you need to depend on multiple synced variables, then use OnDeserialization instead and manually check for if the value changed.

VRChat’s official documentation for OnVariableChange

Syncing

When defining global variables, some types can be synced. A synced variable will ensure that the data is the same for all clients in the instance, including players that join later. When changing a variable, you must own the object in order for it to properly change. If you are not the owner, the value will change for you, but will reset to the owner’s value on the next network update (Deserialization). See networking for more information about networking and ownership. See the Synced Toggle section in Guides for an example usage of synced variables.