A game about forced loneliness, made by TACStudios
1using System.Collections;
2
3namespace Unity.VisualScripting
4{
5 /// <summary>
6 /// Adds an item to a dictionary.
7 /// </summary>
8 [UnitCategory("Collections/Dictionaries")]
9 [UnitSurtitle("Dictionary")]
10 [UnitShortTitle("Add Item")]
11 [UnitOrder(2)]
12 public sealed class AddDictionaryItem : Unit
13 {
14 /// <summary>
15 /// The entry point for the node.
16 /// </summary>
17 [DoNotSerialize]
18 [PortLabelHidden]
19 public ControlInput enter { get; private set; }
20
21 /// <summary>
22 /// The dictionary.
23 /// </summary>
24 [DoNotSerialize]
25 [PortLabel("Dictionary")]
26 [PortLabelHidden]
27 public ValueInput dictionaryInput { get; private set; }
28
29 /// <summary>
30 /// The dictionary with the added element.
31 /// Note that the input dictionary is modified directly then returned.
32 /// </summary>
33 [DoNotSerialize]
34 [PortLabel("Dictionary")]
35 [PortLabelHidden]
36 public ValueOutput dictionaryOutput { get; private set; }
37
38 /// <summary>
39 /// The key of the item to add.
40 /// </summary>
41 [DoNotSerialize]
42 public ValueInput key { get; private set; }
43
44 /// <summary>
45 /// The value of the item to add.
46 /// </summary>
47 [DoNotSerialize]
48 public ValueInput value { get; private set; }
49
50 /// <summary>
51 /// The action to execute once the item has been added.
52 /// </summary>
53 [DoNotSerialize]
54 [PortLabelHidden]
55 public ControlOutput exit { get; private set; }
56
57 protected override void Definition()
58 {
59 enter = ControlInput(nameof(enter), Add);
60 dictionaryInput = ValueInput<IDictionary>(nameof(dictionaryInput));
61 key = ValueInput<object>(nameof(key));
62 value = ValueInput<object>(nameof(value));
63 dictionaryOutput = ValueOutput<IDictionary>(nameof(dictionaryOutput));
64 exit = ControlOutput(nameof(exit));
65
66 Requirement(dictionaryInput, enter);
67 Requirement(key, enter);
68 Requirement(value, enter);
69 Assignment(enter, dictionaryOutput);
70 Succession(enter, exit);
71 }
72
73 private ControlOutput Add(Flow flow)
74 {
75 var dictionary = flow.GetValue<IDictionary>(dictionaryInput);
76 var key = flow.GetValue<object>(this.key);
77 var value = flow.GetValue<object>(this.value);
78
79 flow.SetValue(dictionaryOutput, dictionary);
80
81 dictionary.Add(key, value);
82
83 return exit;
84 }
85 }
86}