NumberFlow, custom nodes

All NumberFlow diagrams have access to a default collection of nodes. But you can also add your own node types. This is useful if you keep using the same set of nodes together, or to add functionality that would otherwise not be possible.

Creating Functions

The first step is to create some functions that can be used as nodes. As functions are identified by their fully qualified type name, it is best to place them in a distinctive namespace. Your classes must extend Function and override the methods Configure and either Compute or ComputeCoordinate.

Below are two example functions. One computes the highest of two floats, and the other computes U plus V.

using CatlikeCoding.NumberFlow;
using UnityEngine;

namespace My.NumberFlow.Functions {
	
	public class HighestFloat : Function {
		
		protected override void Configure () {
			name = "Highest";
			menuName = "Custom/Highest Float";
			returnType = ValueType.Float;
			propertyNames = new string[] { "A", "B" };
			propertyTypes = new ValueType[] { ValueType.Float, ValueType.Float };
		}
		
		public override void Compute (Value output, Value[] arguments) {
			float
				a = arguments[0].Float,
				b = arguments[1].Float;
			output.Float = a >= b ? a : b;
		}
	}
	
	public class UPlusV : Function {
		
		protected override void Configure () {
			name = "U + V";
			menuName = "Custom/U + V";
			returnType = ValueType.Float;
			type = FunctionType.Coordinate;
		}
		
		public override void ComputeCoordinate (Value output, Diagram diagram) {
			output.Float = diagram.uv.x + diagram.uv.y;
		}
	}
}
Here are some things to keep in mind.

Creating a Library

To make your custom functions available as nodes in a NumberFlow diagram, they need to be included in a library. While it's possible to edit the default library and add them there, this is not advised, because updating NumberFlow will reset this library. Instead, you can create your own library asset. You do this by extending FunctionLibrary and registering your functions by overriding the RegisterFunctions method.

using CatlikeCoding.NumberFlow;
using UnityEngine;

namespace My.NumberFlow.Functions {

	public class CustomLibrary : FunctionLibrary {
		
		protected override void RegisterFunctions () {
			Register(new HighestFloat());
			Register(new UPlusV());
		}
	}
}
To be able to create an asset instance of it, you can add an item to the asset menu via a script placed in an Editor folder.

using UnityEditor;
using UnityEngine;

namespace My.NumberFlow.Functions {
	
	public static class MenuItems {
		
		[MenuItem("Assets/Create/My Custom NumberFlow Library")]
		public static void CreateLibrary () {
			CustomLibrary newLibrary = ScriptableObject.CreateInstance<CustomLibrary>();
			AssetDatabase.CreateAsset(newLibrary, "Assets/My Custom NumberFlow Library.asset");
			EditorUtility.FocusProjectWindow();
			Selection.activeObject = newLibrary;
		}
	}
}

Using a Library

You can now use your menu item to create your custom library asset. You only need one. Once you have it, you can drag it to the Function Libraries array in the inspector of a NumberFlow diagram. You will then be able to use your custom nodes in that diagram.
Using custom nodes.
If the diagram at some point loses the reference to your library asset, it will no longer understand your custom nodes. They will only keep working until the diagram is reloaded, for example when adding a node to it. However, it will remember what the nodes are supposed to be. So if you fix the problem it will restore the nodes after another reload-triggering action.