Skip to content

Custom Instructions

Game Creator allows you to very easily create custom Instructions and use them along with the rest.

Programming Knowledge Required

This section assumes you have some programming knowledge. If you don't know how to code, you might be interested in checking out the Game Creator Hub page. Programmers altruistically create custom Instructions for others to download and use in their projects.

Creating an Instruction

The easiest way to create an Instruction C# script is to right click on your Project panel and select Create → Game Creator → Developer → C# Instruction. This will create a template script with the boilerplate structure of an Instruction:

using System;
using System.Threading.Tasks;
using GameCreator.Runtime.Common;
using GameCreator.Runtime.VisualScripting;

[Serializable]
public class MyInstruction : Instruction
{
    protected override Task Run(Args args)
    {
        // Your code here...
        return DefaultResult;
    }
}

Anatomy of an Instruction

An Instruction is a class that inherits from the Instruction super class. The abstract Run(...) method is the entry point of an Instruction's execution, which is automatically called when it's this instruction's time to be executed.

The Run(...) method has a single parameter of type Args, which is a helper class that contains a reference to the game object that initiated the call (args.Self) and the targeted game object (args.Target), if any.

Yielding in Time

Most instructions will be executed in a single frame. However, some instructions might require putting the execution on hold for a certain amount of time, before resuming the execution. The simplest example is with the "Wait Seconds" instruction, which pauses the execution for a few seconds before resuming.

The Instruction super class contains a collection of methods that help with time management.

Async/Await

Instructions use the async/await methodology to manage the flow of an instruction over the course of time. Using the await keyword requires the Run() method to have the async keyword on its method definition:

protected override async Task Run(Args args)
{ }

NextFrame

The NextFrame() method pauses the execution of the Instruction for a single frame, then resumes.

protected override async Task Run(Args args)
{
    await this.NextFrame();
}

Time

The Time(float duration) method pauses the execution of an Instruction for a certain amount of time. The duration parameter is in seconds.

protected override async Task Run(Args args)
{
    await this.Time(5f);
}

While

The While(Func<bool> function) method pauses the execution of an Instruction for as long as the result of the method passed as a parameter returns true. This method is executed every frame and the execution will resume as soon as it returns false.

protected override async Task Run(Args args)
{
    await this.While(() => this.IsPlayerMoving());
}

Until

The Until(Func<bool> function) method pauses the execution of an Instruction for as long as the result of the method passed as a parameter returns false. This method is executed every frame and the execution will resume as soon as it returns true.

protected override async Task Run(Args args)
{
    await this.Until(() => this.PlayerHasReachedDestination());
}

Decoration & Documentation

It is highly recommended to document and decorate the Instruction so it's easier to find and use. It is done using class-type attributes that inform Game Creator of the quirks of this particular instruction.

For example, to set the title of an instruction to "Hello World", use the [Title(string title)] attribute right above the class definition:

using System;
using System.Threading.Tasks;
using GameCreator.Runtime.Common;
using GameCreator.Runtime.VisualScripting;

[Title("Hello World")]
[Serializable]
public class MyInstruction : Instruction
{
    protected override Task Run(Args args)
    {
        // ...
        return DefaultResult;
    }
}

Title

The title of the Instruction. If this attribute is not provided, the title will be a beautified version of the class name.

[Title("Title of Instruction")]

Description

A description of what the Instruction does. This is used both in the floating window documentation and as the description text when uploading an Instruction to the Game Creator Hub.

[Description("Lorem Ipsum dolor etiam porta sem magna mollis")]

Image

The [Image(...)] attribute changes the default icon of the Instruction for one of the default ones. It consists of 2 parameters:

  • Icon [Type]: A Type class of an IIcon derived class. Game Creator comes packed with a lot of icons although you can also create your own.
  • Color [ColorTheme.Type]: The color of the icon. Uses the ColorTheme.Type enum.

For example, one of the icons included is the "Solid Cube" icon. To display a red solid cube as the icon of the instruction, use the following attribute:

[Image(typeof(IconCubeSolid), ColorTheme.Type.Red)]

Category

A sequence of sub-categories organized using the slash (/) character. This attribute helps keep the Instructions organized when the Instructions list dropdown is displayed.

[Category("Category/Sub Category/Name")]

The example above will display the Instruction under the subdirectory Category → Sub Category → Name.

Version

A semantic version to keep track of the development of this Instruction. It's important to note that when updating an Instruction on the Game Creator Hub, the version number must always be higher than the one on the server.

The semantic version follows the standard Major Version, Minor Version, Patch Version. To know more about how semantic versioning works, read the following page: https://semver.org.

[Version(1, 5, 3)]

Parameters

When an Instruction has exposed fields in the Inspector, it's a good idea to document what these do. You can add as many [Parameter(name, description)] attributes as the Instruction has exposed fields.

For example, if the Instruction has these two fields:

public bool waitForTime = true;
public float duration = 5f;

You can document those fields by adding:

[Parameter("Wait For Time", "Whether to wait or not")]
[Parameter("Duration", "The amount of seconds to wait")]

Keywords

Keywords are strings that help the fuzzy finder more easily search for an instruction. For example, the "Change Position" instruction doesn't reference the word "move" or "translate" anywhere in its documentation. However, these words are very likely to reference this instruction when the user types them in the search box.

[Keywords("Move", "Translate")]

Example

The Example attribute allows you to display text as an example of use of this Instruction. There can be more than one [Example(...)] attribute per instruction. This is particularly useful when uploading instructions to the Game Creator Hub.

Markdown

It is recommended to use Markdown notation when writing examples.

[Example("Sed posuere consectetur est at lobortis")]

Multiple Lines

You can use the @ character in front of a string to break the example text into multiple lines. To create a new paragraph, simply add two new lines. For example:

[Example(@"
    This is the first paragraph.
    This is also in the first paragraph, right after the previous sentence.

    This line is part of a new paragraph.
")]

Dependency

This attribute is optional and mainly used in the Game Creator Hub. If this Instruction uses some particular feature of a specific module, it will first check if the user downloading this instruction has that module installed. If the user does not, it will display an error message and forbid downloading it. This is useful to avoid throwing programming errors.

The [Dependency(...)] attribute consists of 4 parameters:

  • Module ID: For example, the ID of the Inventory module is gamecreator.inventory.
  • Major Version: The minimum major version of the dependency module.
  • Minor Version: The minimum minor version of the dependency module.
  • Patch Version: The minimum patch version of the dependency module.
[Dependency("gamecreator.inventory", 1, 5, 2)]

Putting it all together

The following example combines everything explained above. It creates an Instruction that waits a few seconds and then prints a message on the console. Both the message and the amount of seconds to wait are exposed in the Inspector using Properties, so they can be set to a constant value or retrieved from a variable.

using System;
using System.Threading.Tasks;
using GameCreator.Runtime.Common;
using GameCreator.Runtime.VisualScripting;
using UnityEngine;

[Version(1, 0, 0)]

[Title("Shout")]
[Description("Waits a few seconds and then prints a message on the console")]

[Category("Debug/Shout")]
[Keywords("Print", "Log", "Message")]
[Image(typeof(IconCubeSolid), ColorTheme.Type.Yellow)]

[Parameter("Message", "The text that is printed on the console")]
[Parameter("Delay", "The amount of seconds to wait before printing the message")]

[Serializable]
public class InstructionShout : Instruction
{
    [SerializeField] private PropertyGetString m_Message = new PropertyGetString("Hello World");
    [SerializeField] private PropertyGetDecimal m_Delay = new PropertyGetDecimal(1f);

    public override string Title => $"Shout {this.m_Message} after {this.m_Delay}s";

    protected override async Task Run(Args args)
    {
        float delay = (float) this.m_Delay.Get(args);
        await this.Time(delay);

        Debug.Log(this.m_Message.Get(args));
    }
}

Overriding the Title property is optional. It allows you to display a summary of the Instruction's values in the list of Instructions, so you don't have to expand it to know what it does.