Converting To GC Variables
How to convert normal C# variables to Game Creator Variables
The documentation has moved to: https://mitschmr-studios.io/documentation/api-guides/convertingtogcvariables.html
This version will no longer be updated and maintained.
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):
Number (float)
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:
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 topThe 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:
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:
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:
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
:
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:
Original inspector:
New code:
New inspector:
#2
This example focuses on converting a "Graphic" variable to support selecting the target from a Game Creator variable:
Original code:
Original inspector:
New code:
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.
Last updated