Skip to content

Coding_Tips

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

This page is dedicated to a series of tips and tricks for more experienced developers using LUTE.

Editor Code

Most of the code that forms LUTE is known as 'Editor Code' which assumes that the we are building scripts and classes to be used alongside the built in GUI of Unity. For example, you may find that some Orders look different in the Inspector View to how they have been defined in their class (such as custom drop downs and toggle states). This is because we are using 'Editor Code' to define how the serialised properties will be displayed in the Unity GUI.

If we take a look at the order Choice we can see that the properties are not displayed in the Inspector in the same format as they have been defined in the class itself. This is because we have defined the editor side of the class in a separate class: ChoiceEditor.cs. This class derives from OrderEditor which itselfs derives from the Editor class which is a predefined class supplied by the UnityEditor namespace.

The Editor class provides methods that can overrided and allow the inspector to be defined in a custom fashion (OnInspectorGUI).

Typically, to write your own editor scripts for Orders this convention is suitable:

[CustomEditor(typeof(Choice))]
public class ChoiceEditor : OrderEditor

Wider Application

The above is intended to be implemented to customise inspector properties for Orders but this process can be applied to other aspects. For example, both Nodes and Groups editor classes have been defined in other classes to create more user friendly GUI.

We provide multiple methods for creating dropdowns and grabbing properties from other classes to make creating editor code a bit easier for you; the code itself is buried quite deeply in other classes and does require some knowledge of the system - editing such methods and implementations is done at the risk of the developer.

Executing Nodes and Orders

To execute specific Nodes and the consequent Orders found on them, you first need to have a reference to a Flow Engine. The easiest way to do this would be a public Engine property:

using UnityEngine; 
public class MyComponent : MonoBehaviour
{
    public BasicFlowEngine flowEngine;
}

To execute a named Node in the Engine:

flowEngine.ExecuteNode("NodeName");

To start execution at a specific Order index:

flowEngine.ExecuteNode("NodeName", 2);

To tell if an Engine has any executing Nodes:

if (flowEngine.HasExecutingNodes())
{
	// Do something
}

Node Signals

You can use the NodeSignals class to listen for events from the Node execution system.

public MyComponent : MonoBehaviour
{
    void OnEnable() 
    {
    	// Register as listener for Node events
        NodeSignals.OnNodeStart+= OnNodeStart;
    }

    void OnDisable()
    {
    	// Unregister as listener for Node events
        NodeSignals.OnNodeStart-= OnNodeStart;
    }

    void OnNodeStart(Node node)
    {
    	Debug.Log("Node started: " + node._NodeName);
    }
}
Clone this wiki locally