Converting To GC Variables
How to convert normal C# variables to Game Creator Variables
In this tutorial I want to show you how you can convert normal C# variables to Game Creator variables so you can use the scripts in a more versatile way.

Prerequisites

    Install Unity
    Install IDE (I use Visual Studio 2019)
    Import Game Creator
    Import the scripts you want to change

Game Creator Variables

Overview

Game Creator has a powerful variable system which I won't explain in details, you can read more about it on the offical site.
The variable system has 9 different types (each linked with a reference what it is):
This means we can convert variables of the these types to their GC variables counterpart.

Code Perspective

From the code perspective, there are two different things we need to take in view. One is the general variables selection, where you can only select a variable and optionally add a filter of which variable types are allowed. The second one is where you specify a fix variable type and allow the option of adding a value of this type as well (not only GC variables). Let's take a look at the first one:
1
using UnityEngine;
2
using GameCreator.Variables;
3
4
public class GCVariablesTutorial : MonoBehaviour
5
{
6
public VariableProperty variable = new VariableProperty();
7
}
Copied!
This little script gives us this result:
Let's take a look at the code:
    When working with the Game Creator variable system, you need to add the GameCreator.Variables namespace at the top
    The class VariableProperty is a generic variable class that allows you to select any variable of any type, may that be a global, local or list one
As I have written before, you can apply a filter to this type of variable. For this, you need to add this code: [VariableFilter(Variable.DataType.<VariableType>)], where <VariableType> is one of the types mentioned in the overview. Your scripting solution (IDE) should suggest them to you if you don't know them anymore. Let's say, we want to add a filter to only include GameObject variables, the code looks like this:
1
using UnityEngine;
2
using GameCreator.Variables;
3
4
public class GCVariablesTutorial : MonoBehaviour
5
{
6
[VariableFilter(Variable.DataType.GameObject)]
7
public VariableProperty variable = new VariableProperty();
8
}
Copied!
With the selection now looking like this:
We only have one GameObject variable in our global variables, so that's correct.
Now for the second part (which is most likely the more interesting one for you), for each variable type mentioned in the overview, Game Creator has a type that let's you specify a value of this type or a global, local or list variable value. Here you can see what I mean:
In the dropdown, additional to the usual Use Global/Local/List Variable you have the entry Value, which is what I am talking about. The code is this one:
1
using UnityEngine;
2
using GameCreator.Variables;
3
4
public class GCVariablesTutorial : MonoBehaviour
5
{
6
public GameObjectProperty goVariable = new GameObjectProperty();
7
}
Copied!
I have replaced VariableProperty with GameObjectProperty. There are the following classes you can use:
    BoolProperty
    ColorProperty
    NumberProperty
    GameObjectProperty
    SpriteProperty
    StringProperty
    Texture2DProperty
    Vector2Property
    Vector3Property
When you select the dropdown value for selecting a variable and not the Value entry, Game Creator already filters for exactly this type of variable so you can not choose anything else that does not fit.

Variables Access

Even though there is already a page on the official documentation about this topic, it does not cover everything that I am talking about in this guide. That's why there is additional information following.
Getting back to the first type of variables, if you need to access a VariableProperty, you need to call the method Get(invoker), where the optional parameter invoker is of type GameObject and usually the gameobject the script is on. Of course you can also set the invoker to null (which means empty). If we take a look at the previous example of the GameObject, it looks like this:
1
using UnityEngine;
2
using GameCreator.Variables;
3
4
public class GCVariablesTutorial : MonoBehaviour
5
{
6
[VariableFilter(Variable.DataType.GameObject)]
7
public VariableProperty variable = new VariableProperty();
8
9
private void Start()
10
{
11
// Without invoker parameter.
12
GameObject go = (GameObject)this.variable.Get();
13
14
// With invoker parameter set to this gameobject.
15
GameObject go2 = (GameObject)this.variable.Get(this.gameobject);
16
17
// With null invoker parameter set to null.
18
GameObject go3 = (GameObject)this.variable.Get(null);
19
}
20
}
Copied!
You may wonder what the (GameObject) means. The VariableProperty.Get() method returns an object of type object, which we can not use directly. In order for us to use it, we need to convert (cast) it to the desired type (in this case GameObject).
Getting back to the second type of variables, we again take the example of the GameObject, but this time we use GameObjectProperty:
1
using UnityEngine;
2
using GameCreator.Variables;
3
4
public class GCVariablesTutorial : MonoBehaviour
5
{
6
public GameObjectProperty goVariable = new GameObjectProperty();
7
8
private void Start()
9
{
10
// With invoker parameter set to this gameobject.
11
GameObject go = this.goVariable.GetValue(this.gameObject);
12
13
// With invoker parameter set to null.
14
GameObject go2 = this.goVariable.GetValue(null);
15
}
16
}
Copied!
Using the BaseProperty<T>.GetValue() (BaseProperty<T> is the base class of all GC variable classes) method we directly get the value already converted (casted) to the desired type. For instance, you use the NumberProperty class, you get a float value when using the GetValue() method. Note that we can not use this method without the invoker parameter, as this way is now obsolete and no longer supported, it may be removed in the future.
For the variable types BoolProperty, ColorProperty, NumberProperty, StringProperty, Vector2Property and Vector3Property you can set values in the ( ) brackets if desired.

Examples

#1

In this example, we convert three variables (float, GameObject, Color) to their GC counterpart so that we can still set our own values.
Original code:
1
using UnityEngine;
2
3
public class GCVariablesTutorial : MonoBehaviour
4
{
5
public float number = 1;
6
public GameObject go;
7
public Color color = Color.red;
8
}
Copied!
Original inspector:
New code:
1
using UnityEngine;
2
using GameCreator.Variables;
3
4
public class GCVariablesTutorial : MonoBehaviour
5
{
6
public NumberProperty number = new NumberProperty(1);
7
public GameObjectProperty go = new GameObjectProperty();
8
public ColorProperty color = new ColorProperty(Color.red);
9
}
Copied!
New inspector:

#2

This example focuses on converting a "Graphic" variable to support selecting the target from a Game Creator variable:
Original code:
1
public class ActionGraphicColor : IAction
2
{
3
public Graphic graphic;
4
5
[Range(0.0f, 10.0f)]
6
public float duration = 0.0f;
7
8
public ColorProperty color = new ColorProperty(Color.white);
9
10
// EXECUTABLE: ----------------------------------------------------------------------------
11
12
public override bool InstantExecute(GameObject target, IAction[] actions, int index)
13
{
14
if (this.duration <= 0.0f)
15
{
16
if (this.graphic != null) this.graphic.color = this.color.GetValue(target);
17
return true;
18
}
19
20
return false;
21
}
22
23
public override IEnumerator Execute(GameObject target, IAction[] actions, int index)
24
{
25
if (this.graphic != null)
26
{
27
Color currentColor = this.graphic.color;
28
Color targetColor = this.color.GetValue(target);
29
30
float startTime = Time.unscaledTime;
31
WaitUntil waitUntil = new WaitUntil(() =>
32
{
33
float t = (Time.unscaledTime - startTime) / this.duration;
34
this.graphic.color = Color.Lerp(currentColor, targetColor, t);
35
36
return t > 1.0f;
37
});
38
39
yield return waitUntil;
40
this.graphic.color = targetColor;
41
}
42
43
yield return 0;
44
}
45
46
// More code to come, but inspector related, not important for this example.
Copied!
Original inspector:
New code:
1
public class ActionGraphicColor : IAction
2
{
3
public GameObjectProperty graphic = new GameObjectProperty();
4
[Range(0.0f, 10.0f)]
5
public float duration = 0.0f;
6
7
public ColorProperty color = new ColorProperty(Color.white);
8
9
// EXECUTABLE: ----------------------------------------------------------------------------
10
11
public override bool InstantExecute(GameObject target, IAction[] actions, int index)
12
{
13
if (this.duration <= 0.0f)
14
{
15
if (this.graphic != null) this.graphic.GetValue(target).GetComponent<Graphic>().color = this.color.GetValue(target);
16
return true;
17
}
18
19
return false;
20
}
21
22
public override IEnumerator Execute(GameObject target, IAction[] actions, int index)
23
{
24
if (this.graphic != null)
25
{
26
Color currentColor = this.graphic.GetValue(target).GetComponent<Graphic>().color;
27
Color targetColor = this.color.GetValue(target);
28
29
float startTime = Time.unscaledTime;
30
WaitUntil waitUntil = new WaitUntil(() =>
31
{
32
float t = (Time.unscaledTime - startTime) / this.duration;
33
this.graphic.GetValue(target).GetComponent<Graphic>().color = Color.Lerp(currentColor, targetColor, t);
34
35
return t > 1.0f;
36
});
37
38
yield return waitUntil;
39
this.graphic.GetValue(target).GetComponent<Graphic>().color = targetColor;
40
}
41
42
yield return 0;
43
}
44
45
// More code to come, but inspector related, not important for this example.
Copied!
Sharp eyes see the difference. Because the Game Creator variable we use is of type GameObject we have to get the Graphic component and apply the the settings to there.
1
// Old code to access the graphic variable.
2
this.graphic
3
4
// Replaced with.
5
this.graphic.GetValue(target).GetComponent<Graphic>()
6
7
// ----------------------------
8
9
// Old code to access the color property of the graphic variable.
10
this.graphic.color
11
12
// Replaced with.
13
this.graphic.GetValue(target).GetComponent<Graphic>().color
Copied!
Last modified 4mo ago