Skip to content

New_Event_Handlers

Jack Brett edited this page Jun 17, 2024 · 2 revisions

Event Handlers are methods which inform Nodes and behaviours when to execute. We added some basic ones such as Start which mimics the built-in Unity method that will start a given Node when the game initially starts up. Just like with Orders, there is a base parent class which can be inherited for you to create your own Event Handler and setting the relative information on the Event Handler attribute.

Since they are a MonoBehaviour, they can utilise existing Unity messages or your own game systems. Some use Update to check conditions, then call ExecuteNode. We can understand this further by breaking down the GameStarted Event Handler class:

using System.Collections;
using UnityEngine;

[EventHandlerInfo("Default",
                  "Game Started",
                  "Executes when the game starts playing")]
[AddComponentMenu("")]
public class GameStarted : EventHandler
{
    [Tooltip("Wait for a number of frames before executing the node")]
    [SerializeField] protected int waitForFrames = 1;

    protected virtual void Start()
    {
        StartCoroutine(GameStartedCoroutine());
    }

    protected virtual IEnumerator GameStartedCoroutine()
    {
        int frameCount = waitForFrames;
        while (frameCount > 0)
        {
            yield return new WaitForEndOfFrame();
            frameCount--;
        }
        ExecuteNode();
    }

    public override string GetSummary()
    {
        return "This node will execute when the game starts playing";
    }
}
  • EventHandlerInfo Attribute: Adds the EventHandler to the SetEventHandler menu seen on the Node Inspector. The first argument specifies the category, the second sets the name in the menu, and the third provides a tooltip.

  • AddComponentMenu Attribute: Assigning an empty string to this Unity attribute hides the class from the AddComponent menu on regular game objects.

  • Inheritance from EventHandler: A Node can have either no EventHandler or one EventHandler, which dictates when the Node should execute.

  • ExecuteNode Method: A method in the base EventHandler class that triggers the Node to run when all specified conditions are fulfilled.

  • GetSummary Method: This returns a string which will be used to fill out an information box on the Node Inspector below the Event Handler drop-down that informs users of the use of the chosen Event Handler. For example:

eventHandlerInfo

Event Handler Data

As with any MonoBehaviour, fields marked with public or [SerializeField] will be visible in the editor when the component is selected.

For custom variables, use type-specific data structures like IntegerData to allow setting a local value or selecting a compatible variable. When a specific variable type is required, use a Variable field with a VariableProperty attribute to display only compatible variables, such as:

[VariableProperty(typeof(BooleanVariable), typeof(IntegerVariable))]
[SerializeField] protected Variable outValue;
Clone this wiki locally