Beispiel #1
0
        public T GetInputValue <T>(string name, T defaultValue = default)
        {
            var port = GetInputPort(name);

            if (port == null)
            {
                Debug.LogWarning(
                    $"<b>[{this.name}]</b> Unknown input port name `{name}`. Returning default.",
                    this
                    );
                return(defaultValue);
            }

            if (port.connections.Count < 1)
            {
                return(defaultValue);
            }

            NodePort.Connection conn = port.connections[0];
            object output            = conn.node.GetOutputValue(conn.portName);

            if (output == null && typeof(T).IsValueType)
            {
                // Can't ChangeType a null when expecting a value type. Bail.
                Debug.LogWarning(
                    $"<b>[{this.name}]</b> Received null on input port `{name}`. " +
                    $"Expected value type {typeof(T).FullName}. " +
                    $"Returning default.",
                    this
                    );
                return(defaultValue);
            }

            // Short circuit Convert.ChangeType if it's already the expected type
            if (output == null || output.GetType() == typeof(T) || output is T)
            {
                return((T)output);
            }

            try
            {
                return((T)Convert.ChangeType(output, typeof(T)));
            }
            catch (Exception e)
            {
                Debug.LogError(
                    $"<b>[{this.name}]</b> Could not cast output port `{conn.portName}` with type `{output.GetType()}` " +
                    $"to input port `{this.name}` type `{typeof(T)}`. " +
                    $"Returning default.",
                    this
                    );
            }

            return(defaultValue);
        }
Beispiel #2
0
        /// <summary>
        /// Retrieve an input value without attempting a typecast.
        /// </summary>
        public object GetInputValue(string name, object defaultValue = null)
        {
            var port = GetInputPort(name);

            if (port == null)
            {
                Debug.LogWarning(
                    $"<b>[{this.name}]</b> Cannot read unknown port {name}. Returning default."
                    );
                return(defaultValue);
            }

            if (port.connections.Count < 1)
            {
                return(defaultValue);
            }

            NodePort.Connection conn = port.connections[0];
            return(conn.node.GetOutputValue(conn.portName));
        }