diff --git a/Scripts/Node.cs b/Scripts/Node.cs index dba0a01..2a58809 100644 --- a/Scripts/Node.cs +++ b/Scripts/Node.cs @@ -77,7 +77,7 @@ public abstract class Node : ScriptableObject { /// Return all input values for a specified port. Returns fallback value if no ports are connected /// Field name of requested input port /// If no ports are connected, this value will be returned - public T[] GetInputsByFieldName(string fieldName, T[] fallback = default(T[])) { + public T[] GetInputsByFieldName(string fieldName, params T[] fallback) { NodePort port = GetInputByFieldName(fieldName); if (port != null && port.IsConnected) return port.GetInputValues(); else return fallback; diff --git a/Scripts/NodePort.cs b/Scripts/NodePort.cs index 93845cc..5fcd99d 100644 --- a/Scripts/NodePort.cs +++ b/Scripts/NodePort.cs @@ -83,7 +83,8 @@ public class NodePort { /// Return the output value of the first connected port. Returns null if none found or invalid. /// public T GetInputValue() { - return (T)GetInputValue(); + object obj = GetInputValue(); + return obj is T ? (T) obj : default(T); } /// Return the output values of all connected ports. @@ -92,16 +93,22 @@ public class NodePort { object[] objs = GetInputValues(); T[] ts = new T[objs.Length]; for (int i = 0; i < objs.Length; i++) { - ts[i] = (T)objs[i]; + if (objs[i] is T) ts[i] = (T) objs[i]; } return ts; } /// Return true if port is connected and has a valid input. /// - public bool TryGetInputValue(out T value) where T : class { - value = GetInputValue() as T; - return value != null; + 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. @@ -111,7 +118,7 @@ public class NodePort { 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]; + if (objs[i] is float) result += (float) objs[i]; } return result; } @@ -123,7 +130,7 @@ public class NodePort { 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]; + if (objs[i] is int) result += (int) objs[i]; } return result; }