mirror of
https://github.com/Siccity/xNode.git
synced 2025-12-20 09:16:01 +08:00
Improved GetInputsByFieldName<T> usage. Added getvalue type checks
This commit is contained in:
parent
e42622bcf6
commit
0150a7bb81
@ -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>
|
||||
/// <param name="fieldName">Field name of requested input port</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);
|
||||
if (port != null && port.IsConnected) return port.GetInputValues<T>();
|
||||
else return fallback;
|
||||
|
||||
@ -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>
|
||||
/// <returns> <see cref="NodePort.GetOutputValue"/> </returns>
|
||||
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>
|
||||
@ -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;
|
||||
}
|
||||
|
||||
/// <summary> Return true if port is connected and has a valid input. </summary>
|
||||
/// <returns> <see cref="NodePort.GetOutputValue"/> </returns>
|
||||
public bool TryGetInputValue<T>(out T value) where T : class {
|
||||
value = GetInputValue() as T;
|
||||
return value != null;
|
||||
public bool TryGetInputValue<T>(out T value) {
|
||||
object obj = GetInputValue();
|
||||
if (obj is T) {
|
||||
value = (T) obj;
|
||||
return true;
|
||||
} else {
|
||||
value = default(T);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary> Return the sum of all inputs. </summary>
|
||||
@ -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;
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user