Skip to content
Thor Brigsted edited this page Apr 6, 2020 · 12 revisions
class NodeGraph : ScriptableObject

What?

Every xNode project starts with the creation of a graph. The graph contains a list of nodes, and what you decide to do with those nodes is up to you.

You can customize the look of your graphs through a graph editor

Why?

Your graph should act as the topmost entry point for your nodes. Depending on your needs, you can implement methods such as GetFirstNode, GetRootNode, GetResult, UpdateAI etc. inside your graph. It can also be used to contain variables that all your nodes rely on, perhaps a currentNode field for a state machine.

How?

Graph classes are extremely simple. All they have to do is derive from NodeGraph. Ideally you also add the [CreateAssetMenu] attribute so that you are able to create the graph asset through the native asset creation menu in Unity.

[CreateAssetMenu]
public class SimpleGraph : NodeGraph { }

Graph assets cannot reference scene objects. To reference scene objects, take a look at scene graphs.

In this class you can define you own entry points to the graph. For example a method StartGraph() that loops through all nodes and calls Trigger() on those of a certain type.

Nodes are accessible through the nodes variable, and looping through it is your main entrance point to your graph.

[CreateAssetMenu]
public class SimpleGraph : NodeGraph {

  public RootNode GetRootNode() {
    for (int i = 0; i < nodes.Count; i++) {
      if (nodes[i] is NewNode) return nodes[i] as NewNode;
    }
    return null;
  }
}

Tips

Graphs support [ContextMenu] Graphs support the [ContextMenu] attribute. Simply add [ContextMenu] to a non-static method and it will show up when you right-click anywhere on the grid. Select it to execute the method. You can read more about it [here](https://docs.unity3d.com/ScriptReference/ContextMenu.html)
Clone this wiki locally