Variable Instance Types
Allow nodes to create Instances of different types depending on input from the NodeTemplate.
This feature builds on the changes proposed in #9 that make the NodeTemplate a source of data for the Instance rather than making the NodeTemplate setup the Instance.
public class MathNode : Node<MathNode>
{
public InputSocket ValueA;
public InputSocket ValueB;
public OutputSocket Output;
public bool Negate;
public override Instance Create ()
{
if (Negate)
{
return new MinusInstance ();
}
else
{
return new AddInstance ();
}
}
public class AddInstance : Instance
{
public Input<float> ValueA;
public Input<float> ValueB;
public Output<float> Output;
public override InputMap[] Inputs (IGraphConnections graph, AddNode node) => new[]
{
graph.Connect(ref node.ValueA, ref ValueA),
graph.Connect(ref node.ValueB, ref ValueB)
};
public override OutputMap[] Outputs (IGraphConnections graph, AddNode node) => new[]
{
graph.Connect(ref node.Output, ref Output)
};
public override void Setup (IGraphInstance graph)
{
// Subscribe to events, setup variables...
}
public override void OnInputChanged ()
{
// Fires when an input value changes.
Output.Value = ValueA.Value + ValueB.Value;
}
public override void Remove ()
{
// Unsubscribe from events, dispose members...
}
}
public class MinusInstance : Instance
{
public Input<float> ValueA;
public Input<float> ValueB;
public Output<float> Output;
public override InputMap[] Inputs (IGraphConnections graph, AddNode node) => new[]
{
graph.Connect(ref node.ValueA, ref ValueA),
graph.Connect(ref node.ValueB, ref ValueB)
};
public override OutputMap[] Outputs (IGraphConnections graph, AddNode node) => new[]
{
graph.Connect(ref node.Output, ref Output)
};
public override void Setup (IGraphInstance graph)
{
// Subscribe to events, setup variables...
}
public override void OnInputChanged ()
{
// Fires when an input value changes.
Output.Value = ValueA.Value - ValueB.Value;
}
public override void Remove ()
{
// Unsubscribe from events, dispose members...
}
}
}
For the most part, I consider supporting this a low-priority feature. It would require that the instance serialization support the type it's deserializing into potentially changing, which may be inefficient.
Editor Integration
The benefit of this feature might show itself in the ability to have generic-typed instances. This, however, might making automatic editor-integration quite hard.
Perhaps the different instance types that a node could create would be better defined using a serializable data structure rather than code, as this structure could be imported into the editor and change the socket types as required.