Variable Setter


Node Information
Sets specified variable with the specified value.
General Information
• This node doesn't have other available options.
• Some of the ports of this node can change their types and accept types automatically.
Visual
Visual

Ports
Text Type DataType Accepts IsList CustomAcception
Set IN RUN MULTIPLE FALSE FALSE
Variable IN String ONE FALSE TRUE
Value IN String ONE FALSE TRUE

Source
namespace Korduene.KGraph.Nodes.Variables
{
    [IsNode("Variable Setter")]
    public class VarSetter : Node
    {
        Port run = new Port(PortType.IN, typeof(RUN), AcceptsLinks.MULTIPLE, "Set");
        Port val = new Port(PortType.IN, typeof(string), AcceptsLinks.ONE, "Value");
        Port variable = new Port(PortType.IN, typeof(string), AcceptsLinks.ONE, "Variable");
        public VarSetter()
        {
            NodeType = KGraph.NodeType.Method;
            this.Name = "Variable Setter";
            val.Value = "0";
            variable.Value = "0";
            AddPort(run);
            AddPort(variable);
            AddPort(val);
            variable.CustomAcception = VariableAccepts;
            val.CustomAcception = ValAccepts;
            Information = "Sets specified variable with the specified value.";
        }
        bool VariableAccepts(Port port)
        {
            if (!variable.IsConnected && !val.IsConnected)
            {
                variable.DataType = port.DataType;
                val.DataType = port.DataType;
                return true;
            }
            if (!variable.IsConnected && val.IsConnected && variable.DataType == port.DataType)
            {
                return true;
            }
            return false;
        }
        bool ValAccepts(Port port)
        {
            if (val.DataType == port.DataType)
            {
                return true;
            }
            return false;
        }
        public override string Code()
        {
            if (variable.IsConnected)
            {
                return GetValue(variable) + " = " + GetValue(val) + ";";
            }
            return string.Empty;
        }
    }
}