diff --git a/Scripts/Node.cs b/Scripts/Node.cs
index 90b65e3..a280ff5 100644
--- a/Scripts/Node.cs
+++ b/Scripts/Node.cs
@@ -184,10 +184,24 @@ namespace XNode {
/// Returns a value based on requested port output. Should be overridden in all derived nodes with outputs.
/// The requested port.
- public virtual object GetValue(NodePort port) {
- Debug.LogWarning("No GetValue(NodePort port) override defined for " + GetType());
- return null;
+ public object GetValue(NodePort port) {
+ if (port.targetDelegate == null) port.Initialize(GetDelegate);
+ return port.targetDelegate.DynamicInvoke();
}
+
+ /// Returns a value based on requested port output. Should be overridden in all derived nodes with outputs.
+ /// The requested port.
+ public T GetValue(NodePort port) {
+ if (port.targetDelegate == null) port.Initialize(GetDelegate);
+ Func func = port.targetDelegate as Func;
+
+ if (func != null)
+ return func();
+
+ return default(T); //default
+ }
+
+ public abstract Delegate GetDelegate(string fieldName);
#endregion
/// Called after a connection between two s is created
diff --git a/Scripts/NodePort.cs b/Scripts/NodePort.cs
index 0ada72d..6b4c441 100644
--- a/Scripts/NodePort.cs
+++ b/Scripts/NodePort.cs
@@ -26,6 +26,7 @@ namespace XNode {
public bool IsConnected { get { return connections.Count != 0; } }
public bool IsInput { get { return direction == IO.Input; } }
public bool IsOutput { get { return direction == IO.Output; } }
+ public Delegate targetDelegate;
public string fieldName { get { return _fieldName; } }
public Node node { get { return _node; } }
@@ -88,6 +89,12 @@ namespace XNode {
_connectionType = connectionType;
}
+ /// Initialize the delegate for this NodePort.
+ /// Supply the GetDelegate method in the parent node.
+ public void Initialize(Func initializer) {
+ targetDelegate = initializer(fieldName);
+ }
+
/// Checks all connections for invalid references, and removes them.
public void VerifyConnections() {
for (int i = connections.Count - 1; i >= 0; i--) {
@@ -101,23 +108,23 @@ namespace XNode {
/// Return the output value of this node through its parent nodes GetValue override method.
///
- public object GetOutputValue() {
- if (direction == IO.Input) return null;
- return node.GetValue(this);
+ public T GetOutputValue() {
+ if (direction == IO.Input) return default(T);
+ return node.GetValue(this);
}
/// Return the output value of the first connected port. Returns null if none found or invalid.
///
- public object GetInputValue() {
+ public T GetInputValue() {
NodePort connectedPort = Connection;
- if (connectedPort == null) return null;
- return connectedPort.GetOutputValue();
+ if (connectedPort == null) return default(T);
+ return connectedPort.GetOutputValue();
}
/// Return the output values of all connected ports.
///
- public object[] GetInputValues() {
- object[] objs = new object[ConnectionCount];
+ public T[] GetInputValues() {
+ T[] ts = new T[ConnectionCount];
for (int i = 0; i < ConnectionCount; i++) {
NodePort connectedPort = connections[i].Port;
if (connectedPort == null) { // if we happen to find a null port, remove it and look again
@@ -125,66 +132,11 @@ namespace XNode {
i--;
continue;
}
- objs[i] = connectedPort.GetOutputValue();
- }
- return objs;
- }
-
- /// Return the output value of the first connected port. Returns null if none found or invalid.
- ///
- public T GetInputValue() {
- object obj = GetInputValue();
- return obj is T ? (T) obj : default(T);
- }
-
- /// Return the output values of all connected ports.
- ///
- public T[] GetInputValues() {
- object[] objs = GetInputValues();
- T[] ts = new T[objs.Length];
- for (int i = 0; i < objs.Length; i++) {
- if (objs[i] is T) ts[i] = (T) objs[i];
+ ts[i] = connectedPort.GetOutputValue();
}
return ts;
}
- /// Return true if port is connected and has a valid input.
- ///
- public bool TryGetInputValue(out T value) {
- object obj = GetInputValue();
- if (obj is T) {
- value = (T) obj;
- return true;
- } else {
- value = default(T);
- return false;
- }
- }
-
- /// Return the sum of all inputs.
- ///
- public float GetInputSum(float fallback) {
- object[] objs = GetInputValues();
- if (objs.Length == 0) return fallback;
- float result = 0;
- for (int i = 0; i < objs.Length; i++) {
- if (objs[i] is float) result += (float) objs[i];
- }
- return result;
- }
-
- /// Return the sum of all inputs.
- ///
- public int GetInputSum(int fallback) {
- object[] objs = GetInputValues();
- if (objs.Length == 0) return fallback;
- int result = 0;
- for (int i = 0; i < objs.Length; i++) {
- if (objs[i] is int) result += (int) objs[i];
- }
- return result;
- }
-
/// Connect this to another
/// The to connect to
public void Connect(NodePort port) {