Properties¶
Game Creator properties are a special type of class that allows to dynamically specify the source of a field value using a dropdown menu. The menu's options are dynamic and can be added without the need of overwriting Game Creator core code, allowing to write maintainable and decoupled code.
Polymorphic Serialization
Properties take advantage of Unity's polymorphic serialzation, which means that the dropdown menu options are decoupled from the core code. Anyone can plug in their own menu options without overwriting any scripts.
There are different types of Properties, each with its own set of options. All of them have in common that, when retrieving them, an instance of Args
parameter is passed, which contains two fields:
- Target: A reference to the
Game Object
responsible for calling the property - Self: A reference to the
Game Object
containing the property reference.
Args Parameter
There are some cases where the Target
and Self
fields will reference the same game object.
Property Get types allow to retrieve a value and Property Set types allow to set a value. Game Creator comes with a collection of both types, but each module increases the amount available. You can even create your own property types to extend the existing ones.
Property Types¶
There are a multiple default property types available, which start with PropertyGet ~
and end with the type. For example PropertyGetNumber
is the property type for numeric values, and PropertyGetBool
is the analog for boolean (true/false) values.
There are also property setters, which allow to set the value of a property. They start with PropertySet ~
and also end with the type name. For example the PropertySetGameObject
allows to set the value of a game object reference.
Using Properties¶
UI Toolkit
Using properties requires the Editor scripts to be written using Unity's UI Toolkit. IMGUI is not supported.
To use a property it's very simple. You just need to declare them as you would with a primitive type, but instead of getting the value directly, call the Get(args)
method to retrieve its value.
For example, let's say that in a component, you want to get a string
value. Instead of declaring a value like this:
public string myValue = "This is my string";
You could use a property so the source of that string value isn't hard-coded, but set from the Inspector. Like this:
public PropertyGetString myValue = new PropertyGetString();
This will display a dropdown menu on the Inspector with the current option selected. By default it's a constant string, but the value can be chosen to come from the name of a game object, a local or global variable, etc.
To get the value you simply call the Get(args)
method:
string value = this.myValue.Get(args);
Args
The Args
(arguments) class is a two-field struct that contains the game object considered as the source of the call as well as the targeted game object. This class is necessary in order to use properties that reference the "Self" or "Target" values. If you are not sure what the self and target objects are, simply pass in the current MonoBehaviour's game object:
Args args = new Args(this.gameObject);
Creating Properties¶
Just like Instructions and other visual scripting nodes, one can create custom properties to interact with other assets or custom code. To create a new GET Property simply inherit the PropertyTypeGet[TYPE]
.
Example of a custom number property
For example let's say we want to create a custom number GET property that always returns 42 (for some reason). In that case we create a new script that inherits from PropertyTypeGetNumber
called GetNumber42
:
public class GetNumber42 : PropertyTypeGetNumber
{
public override Vector3 Get(Args args)
{
return 42;
}
}
Adding fields
You can also expose fields just like you would do in a custom Inspector script. For example, if you want to display an integer field which will be returned by the property you can do so:
public class GetNumberInteger : PropertyTypeGetNumber
{
[SerializedField] private int myNumber = 42;
public override Vector3 Get(Args args)
{
return this.myNumber;
}
}
You can add as many fields as you want. Even other properties.
To return the value the Get(args)
method must be implemented. The Args
parameter contains the Self
and Target
values calling the property, which can be used to dynamically get the final value if necessary.
Optionally the PropertyTypeGetNumber
child class can also override the String
property to display a nicer title in the Unity Inspector. For example:
public class GetNumber42 : PropertyTypeGetNumber
{
public override Vector3 Get(Args args)
{
return 42;
}
public override string String => "42";
}
Property classes can also be decorated with the Title
, Category
, Description
and other attributes, just like it is done on Instructions and other visual scripting nodes.