1
0
mirror of https://github.com/Siccity/xNode.git synced 2025-12-20 17:26:02 +08:00

Improved GetInputsByFieldName<T> usage. Added getvalue type checks

This commit is contained in:
Thor Brigsted 2017-10-28 19:03:21 +02:00
parent e42622bcf6
commit 0150a7bb81
2 changed files with 15 additions and 8 deletions

View File

@ -77,7 +77,7 @@ public abstract class Node : ScriptableObject {
/// <summary> Return all input values for a specified port. Returns fallback value if no ports are connected </summary> /// <summary> Return all input values for a specified port. Returns fallback value if no ports are connected </summary>
/// <param name="fieldName">Field name of requested input port</param> /// <param name="fieldName">Field name of requested input port</param>
/// <param name="fallback">If no ports are connected, this value will be returned</param> /// <param name="fallback">If no ports are connected, this value will be returned</param>
public T[] GetInputsByFieldName<T>(string fieldName, T[] fallback = default(T[])) { public T[] GetInputsByFieldName<T>(string fieldName, params T[] fallback) {
NodePort port = GetInputByFieldName(fieldName); NodePort port = GetInputByFieldName(fieldName);
if (port != null && port.IsConnected) return port.GetInputValues<T>(); if (port != null && port.IsConnected) return port.GetInputValues<T>();
else return fallback; else return fallback;

View File

@ -83,7 +83,8 @@ public class NodePort {
/// <summary> Return the output value of the first connected port. Returns null if none found or invalid. </summary> /// <summary> Return the output value of the first connected port. Returns null if none found or invalid. </summary>
/// <returns> <see cref="NodePort.GetOutputValue"/> </returns> /// <returns> <see cref="NodePort.GetOutputValue"/> </returns>
public T GetInputValue<T>() { public T GetInputValue<T>() {
return (T)GetInputValue(); object obj = GetInputValue();
return obj is T ? (T) obj : default(T);
} }
/// <summary> Return the output values of all connected ports. </summary> /// <summary> Return the output values of all connected ports. </summary>
@ -92,16 +93,22 @@ public class NodePort {
object[] objs = GetInputValues(); object[] objs = GetInputValues();
T[] ts = new T[objs.Length]; T[] ts = new T[objs.Length];
for (int i = 0; i < objs.Length; i++) { 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 ts;
} }
/// <summary> Return true if port is connected and has a valid input. </summary> /// <summary> Return true if port is connected and has a valid input. </summary>
/// <returns> <see cref="NodePort.GetOutputValue"/> </returns> /// <returns> <see cref="NodePort.GetOutputValue"/> </returns>
public bool TryGetInputValue<T>(out T value) where T : class { public bool TryGetInputValue<T>(out T value) {
value = GetInputValue() as T; object obj = GetInputValue();
return value != null; if (obj is T) {
value = (T) obj;
return true;
} else {
value = default(T);
return false;
}
} }
/// <summary> Return the sum of all inputs. </summary> /// <summary> Return the sum of all inputs. </summary>
@ -111,7 +118,7 @@ public class NodePort {
if (objs.Length == 0) return fallback; if (objs.Length == 0) return fallback;
float result = 0; float result = 0;
for (int i = 0; i < objs.Length; i++) { 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; return result;
} }
@ -123,7 +130,7 @@ public class NodePort {
if (objs.Length == 0) return fallback; if (objs.Length == 0) return fallback;
int result = 0; int result = 0;
for (int i = 0; i < objs.Length; i++) { 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; return result;
} }