Skip to content

Dynamic Ports

Thor Brigsted edited this page Apr 22, 2020 · 4 revisions
class NodePort

What?

A dynamic port (also known as an instance port) is a special kind of port. They are used for the same things as a static port, but the difference is they can be added and removed from a node freely during edit- and run-time.

Since dynamic ports aren't tied to a serializable field, you may need to do so through a node editor.

Why?

The reasons for using dynamic ports may vary. You might want to show a port which has the type of a class Unity doesn't serialize. Dynamic ports can be of any type. You might want to be able to add and remove ports dynamically. InstancePortList can help you with this. Dynamic ports also do not require a serializable field to be present in the script.

How?

You can add dynamic ports freely through an instance of a Node.

For the most common uses you might want to do it through a NodeEditor script.

The API should look something like this:

void AddDynamicPorts(Node node) {  
 node.AddDynamicInput(typeof(string), fieldName: "myDynamicInput");   
 node.AddDynamicOutput(typeof(int), fieldName: "myDynamicOutput");
}

void RemoveDynamicPorts(Node node) {  
 node.RemoveDynamicPort("myDynamicInput");   
 node.RemovDynamicPort("myDynamicOutput");
}

As with normal ports, dynamic ports can be drawn on the node through a node editor using the method

NodeEditorGUILayout.DrawPort(myDynamicPort);

Technically...

They are stored in the same array as static ports. The only thing that sets the two apart is a bool.

When Unity recompiles, xNode performs a check across all Node types and synchronizes the serialized ports with the [Input] and [Output] attributes, deleting serialized ports that are no longer present in the script, and serializing new ones that may have been added. This step is ignored for dynamic ports.

Clone this wiki locally