using System.Collections; namespace Unity.VisualScripting { /// /// Adds an item to a dictionary. /// [UnitCategory("Collections/Dictionaries")] [UnitSurtitle("Dictionary")] [UnitShortTitle("Add Item")] [UnitOrder(2)] public sealed class AddDictionaryItem : Unit { /// /// The entry point for the node. /// [DoNotSerialize] [PortLabelHidden] public ControlInput enter { get; private set; } /// /// The dictionary. /// [DoNotSerialize] [PortLabel("Dictionary")] [PortLabelHidden] public ValueInput dictionaryInput { get; private set; } /// /// The dictionary with the added element. /// Note that the input dictionary is modified directly then returned. /// [DoNotSerialize] [PortLabel("Dictionary")] [PortLabelHidden] public ValueOutput dictionaryOutput { get; private set; } /// /// The key of the item to add. /// [DoNotSerialize] public ValueInput key { get; private set; } /// /// The value of the item to add. /// [DoNotSerialize] public ValueInput value { get; private set; } /// /// The action to execute once the item has been added. /// [DoNotSerialize] [PortLabelHidden] public ControlOutput exit { get; private set; } protected override void Definition() { enter = ControlInput(nameof(enter), Add); dictionaryInput = ValueInput(nameof(dictionaryInput)); key = ValueInput(nameof(key)); value = ValueInput(nameof(value)); dictionaryOutput = ValueOutput(nameof(dictionaryOutput)); exit = ControlOutput(nameof(exit)); Requirement(dictionaryInput, enter); Requirement(key, enter); Requirement(value, enter); Assignment(enter, dictionaryOutput); Succession(enter, exit); } private ControlOutput Add(Flow flow) { var dictionary = flow.GetValue(dictionaryInput); var key = flow.GetValue(this.key); var value = flow.GetValue(this.value); flow.SetValue(dictionaryOutput, dictionary); dictionary.Add(key, value); return exit; } } }