Siccity / xNode

Unity Node Editor: Lets you view and edit node graphs inside Unity
MIT License
3.34k stars 593 forks source link

Ints aren't floats, but are floats, but aren't #379

Open Draco18s opened 1 year ago

Draco18s commented 1 year ago

https://github.com/Siccity/xNode/blob/82f78879316817dc5c7f8703352f5660a22ec3d4/Scripts/NodePort.cs#L155-L158

What happens if GetInputValue() returns an integer and the port has a type T that is a float?

What value is returned by this method?

Answer: The returned value is `0` (and always and only `0`) because `integer is not float` and thus the check is False and the default value is returned. However, one would expect that an integer *can* be cast to a float, returning `(T)obj` instead. ~~The correct line on 157 should be `return obj != null && obj.GetType().IsInstanceOfType(typeof(T)) ? (T)obj : default(T);`~~ I get this part wrong all the time. I *meant* `IsAssignableFrom` but that doesn't actually work here either. Had to do this: ```csharp public T GetInputValue() { object obj = GetInputValue(); if (typeof(IConvertible).IsAssignableFrom(typeof(T)) && obj is IConvertible) { return (T)Convert.ChangeType(obj, typeof(T)); } return obj is T ? (T)obj : default(T); } ```