Variable References
Introduction
Variable References allows any variable on an instance or component to reference other variables. These other variables can be on the same instance or component, a different instance, or even variables from a different component.

One common use of Variable References is to create a centralized style component which can be referenced throughout a Gum project.
Variables which are assigned through Variable References cannot be directly set on the instance - the value obtained through the reference overwrites any custom value. For example, the reference Height = Width results in Height being read-only and depending on Width. Note that this is using a shorthand variable assignment which is discussed in later sections.

Variable references can also be re-evaluated at runtime to support theming and dynamic styling. For details, see Runtime Variable References.
Variable references mimic the C# syntax, but provide only a subset of C# functionality. Future versions of Gum may expand supported syntax. If your project requires additional functionality, please post an issue on GitHub, or make a request in the Discord server.
Variable Reference Syntax
Variable References can contain multiple lines. Each line is a separate variable reference. Each variable reference uses the following syntax:
For example, to assign a Component's X value to a different component's X value, the following syntax could be used:
Variable Assignment Reference
X references the X value in OtherComponent. Note that X could be the X value on any component or any instance inside of a component:
Spaces are optional around the equals sign, but spaces are not allowed in variable names. The following lines are okay:
However, the following is not allowed:
Previous versions of Gum included spaces in some variable names. The public release of Gum in February 2025 has removed all spaces to simplify the syntax.
For more information, see the Removal of Variable Spaces page.
Lines can be commented out to disable the reference. If Gum encounters a scripting error, it will automatically comment out lines as well so that you can make corrections:
An Instance can reference its own Component's X value by using the qualified name:
An instance or component can reference the variable of another instance in the same component by using the name of the instance. The name of the containing component or screen is not required if the instances are both in the same component or screen:
Components and instances can reference variables that are contained within instances of other components. In this case the name of the referenced instance is appended to the qualified name of the Screen or Component:
Elements and Screens inside subfolders can be referenced. The subfolder path is included with forward slashes:
The right side can be a variable in a Screen, although this isn't too common in practice:
Similarly, variables on Standards can also be referenced. This is also quite rare, since this modifying the Standard element also has side effects on every other instance that is of the same type:
Although it's common for variables to reference the same variable on a different object (such as X being set to another object's X value), this is not a requirement. For example, the following lines are valid:
Instances can reference their own variables, but these must be qualified. For example, ColoredRectangleInstance can assign its Width to equal its own Height. Note that Gum will extend shorthand code as shown in a later section:
Variables can be assigned to constant values, essentially locking the value:
Math operations can be used in variable assignments. This includes add, subtract, multiply, divide, and parenthesis to control order of operations:
Math operations can reference both constant values (1, 2, 3) or other variables:
Conditional and Logical Expressions
Variable references support conditional (ternary) expressions, comparison operators, and logical operators. These can be combined to drive variable values from boolean conditions or to switch between values based on other variables.
Conditional (Ternary) Expressions
A conditional expression uses the C# cond ? a : b syntax. If cond evaluates to true, the result is a; otherwise the result is b. The condition can be a boolean variable or a comparison:
Just like simple variable references, the right side can be fully qualified to point at another element:
Ternary expressions can be nested. Use parenthesis to make the order of evaluation clear:
Comparison Operators
The operators ==, !=, <, >, <=, and >= can be used to compare values. The result is a bool, so comparisons are most often used as the condition of a ternary or as the right side of a bool assignment:
Logical Operators
The logical operators && (and) and || (or) combine two bool values into a single bool. Both operands must be bool:
Boolean Negation
The ! operator inverts a bool value:
Category-State Assignment
Variable references can drive a component's categorical state by assigning to <CategoryName>State. The right side must resolve to a string matching one of the state names defined in that category. This is useful for switching visual states from another instance's variable. For example, on a button-derived component with a ButtonCategory containing Enabled and Disabled states:
If the right side is a literal string, Gum validates it against the category's state names and comments out the line if there is no match.
Enum Assignment
Variables whose type is an enumeration — such as ChildrenLayout, or unit and alignment types like XUnits and WidthUnits — can be assigned using the enum value's name as a string. Gum converts the name to the matching enum value:
Because the right side is a string, an enum can also be selected with a ternary or driven from another variable. For example, a container can switch its layout direction based on a bool:
The name must match one of the enum's values; if it does not, the assignment is not applied. An enum variable can also be assigned from another variable of the same enum type, or from the enum's underlying integer value:
Gum treats the prefixes Components/, Screens/, and Standards/ as special prefixes and does not consider this to be a division operator. Therefore, you should not name variables Components, Screens, or Standards as these are reserved words. Other variable references can freely use the forward slash character to create division.
For example, the following two lines of code show the difference between a simple variable reference assignment and a division operation:
Numeric variable types can be mixed. For example, Red is typically an int value (whole number), while X is a float (supports decimals). Gum automatically casts the variable appropriately:
Gum automatically casts any value to a string (text). For example, a Text's Text variable could be assigned to its own Y value:
Strings can be concatenated (combined) using the + operator:
Gum cannot convert unrelated types, including different Units. For example, the following would be commented out by Gum:
Referencing Custom Variables
Variable references can include custom variables, both on the left and right side. Custom variables can be combined with variable references to create flexible layouts. For more information on using custom variables, see the Add Variables page.
Unqualified and Shorthand Assignments
As mentioned above, fully qualified assignments are allowed in any context. For example, a component can reference its own qualified variables:
Components do not need to qualify their own variables. They can reference them without any qualification. Therefore, the following variable reference is equivalent to the fully-qualified reference above:
Similarly, the following two variable references are equivalent assuming ContainedInstance is part of the component with the reference:
Instances must qualify their own variables as shown in the following code:
Gum will automatically qualify assignments when an instance is selected. In other words, X = Y gets qualified to X = SameInstance.Y if SameInstance is the owner of the variable. This automatic qualification makes it easy for an instance to reference its own values. The following animation shows how the Y and Height values become qualified to the instance after tabbing out of the Variable Reference text box.

Complex assignments are automatically qualified as well.

The left side of an assignment can be omitted if referencing the same variable on another instance or component. For example, by typing OtherInstance.YUnits , Gum automatically expands the reference to YUnits = OtherInstance.YUnits .

Note that this only works when assigning one variable directly to another variable. Complex assignments will not be prefixed.
Color Expansion
Assigning color values is a common part of styling, so to help with this situation, Gum also expands the "Color" variable into all three components when the Variable References text box loses focus. For example, the following text can be used to assign all three values at once:
When the Variable References box loses focus, this is expanded to the following assignments:

Variable References in the Property Grid
As shown above, Variable References can be used to assign one variable to another. If a variable is referenced, then the variable cannot be manually assigned. The Variable Reference takes priority. For example, if an object references the Red, Green, and Blue variables, then those values cannot be manually set on the object. The values appear disabled and text indicates why they are read-only.

Obtaining a Qualified Variable Name
Typing a variable name can be tedious, especially when referencing a variable in a different Screen or Component. Qualified variable names can be obtained by right-clicking on the variable name in Gum and selecting the Copy Qualified Variable Name option. This can then be pasted in the Variable References box of any other object.

Example - Creating Color Styles
The following example creates a Styles component which contains a color value which is referenced by objects in a MainMenu Screen.
Any component can serve as a centralized location for styling, but we use the name Styles by convention.
The Styles component can contain as many objects as are needed to style your project. Additional objects can be added to help indicate how things are used visually. For example, we include a Text object to indicate the red color is the Primary Color.

The color value can be referenced by any other object including objects in different screens or components.
To add a variable reference:
Select the object which should have a variable reference
Click inside the Variable References text box
Type the variable reference. The format of the variable reference is
{VariableName} = {Components or Screens}/{ComponentOrScreenName}.{InstanceName}.{InstanceVariable}For example, to reference the Red variable in the Styles component, the syntax isRed = Components/Styles.PrimaryColor.Red\
Since color values have three components (Red, Green, and Blue), then all three components must be referenced. In this example, the background can reference the three colors with the following assignment text:

The types of the objects that contain the Variable References or which are being referenced do not matter. For example, a Text object could have its color values depend on the color values defined by a ColoredRectangle in the Styles component.

Once Variable References are set, the referenced instances (instances in Styles) can be changed and the changes will immediately propagate throughout the entire project.

Example: Semantic Color Categories on Standard Elements
The example above references a Styles component from one object at a time. You can go further and build a reusable, semantic color palette (for example: White, Gray, Black, Primary, Success, Warning, Danger) that any instance in your project can select by name, without hardcoding a color anywhere outside of the Styles component.
This combines two features already covered elsewhere:
A category per Standard Element - Select the
NineSlice,Sprite, orTextStandard Element and add a category (for exampleColorCategory) with one state per palette name. See Categories for how to create categories and states.A Variable Reference per state - Select each state in the category and use the Color Expansion shorthand to point it at the matching instance in your Styles component:
Repeat this for every state in the category (White, Gray, Primary, Success, and so on), pointing each one at its matching Styles instance. Because the reference lives on the Standard Element itself, every instance of that Standard Element across your entire project shares the same palette.
Add a screenshot showing the ColorCategory states on the NineSlice Standard Element, each with a Color Expansion reference into the Styles component.
Repeat the category on each Standard Element that needs coloring (NineSlice, Sprite, Text). Once this is set up, any instance anywhere in the project can select a palette color with a single category-state assignment instead of a color reference:
This pattern is also what makes the palette recolorable from game code at runtime - for example, letting a game read player-customizable colors from settings and apply them on startup. See Runtime Variable References.
Last updated
Was this helpful?

