Skip to content

Getting Started

Thor Brigsted edited this page Mar 31, 2018 · 18 revisions

Getting started with your own node system is quick and easy. Summarized, a graph consists of a NodeGraph with any amount of Nodes inside. Both classes derive from ScriptableObject, allowing them to exist as assets inside your Project folder.

Creating a NodeGraph

You need a NodeGraph before you can make any nodes. The NodeGraph is a very simple class mainly for manipulation and evaluation of the nodes inside.

A simple NodeGraph script can look like this:

// SimpleGraph.cs

[CreateAssetMenu]
public class SimpleGraph : NodeGraph { }

Tip: Quickly create a NodeGraph script by selecting Assets > Create > xNode > NodeGraph C# Script

The [CreateAssetMenu] lets you create an asset file in your Project window through Right-Click > Create > SimpleGraph. More info

Nothing else is required for the NodeGraph to work, although many methods area available to overload, giving you control over appearance as well as user experience.

API reference of the NodeGraph can be found Here

Creating a Node

Nodes are like regular ScriptableObjects, except they are editable through a NodeGraph, and let you create and access inputs and outputs. To create a node, simply derive from the Node class.

// SimpleNode.cs

public class SimpleNode : Node { 
  [Input] public float a;
  [Output] public float b;

  public override object GetValue(NodePort port) {
    if (port.fieldName == "b") return GetInputValue<float>("a", a);
    else return null;
  }
}

Tip: Quickly create a Node script by selecting Assets > Create > xNode > Node C# Script

[Input] and [Output] lets you define static ports for the node which can be connected to other node ports. The field name is cached and used to access the NodePort at any time using methods such as NodePort GetInputPort(string fieldName) and T GetInputValue<T>(string fieldName, T fallback). These ports will only show on serialized fields.

For all nodes with output ports, it is important to override GetValue(NodePort port), as this will be called when using GetInputValue. The requested NodePort is passed and the returned data will be passed to the asking node. In this example we check if the fieldName of the requested node port is "b". If it is, we return the value of "a". Since there is only one output, such a check is largely unnecessary, as no other ports will ever be requested.

API reference of the Node can be found Here

Clone this wiki locally