A game about forced loneliness, made by TACStudios
1using UnityEngine;
2
3namespace Unity.VisualScripting
4{
5 /// <summary>
6 /// Assigns the value of a variable.
7 /// </summary>
8 [UnitShortTitle("Set Variable")]
9 public sealed class SetVariable : UnifiedVariableUnit
10 {
11 /// <summary>
12 /// The entry point to assign the variable.
13 /// </summary>
14 [DoNotSerialize]
15 [PortLabelHidden]
16 public ControlInput assign { get; set; }
17
18 /// <summary>
19 /// The value to assign to the variable.
20 /// </summary>
21 [DoNotSerialize]
22 [PortLabel("New Value")]
23 [PortLabelHidden]
24 public ValueInput input { get; private set; }
25
26 /// <summary>
27 /// The action to execute once the variable has been assigned.
28 /// </summary>
29 [DoNotSerialize]
30 [PortLabelHidden]
31 public ControlOutput assigned { get; set; }
32
33 /// <summary>
34 /// The value assigned to the variable.
35 /// </summary>
36 [DoNotSerialize]
37 [PortLabel("Value")]
38 [PortLabelHidden]
39 public ValueOutput output { get; private set; }
40
41 protected override void Definition()
42 {
43 base.Definition();
44
45 assign = ControlInput(nameof(assign), Assign);
46 input = ValueInput<object>(nameof(input)).AllowsNull();
47 output = ValueOutput<object>(nameof(output));
48 assigned = ControlOutput(nameof(assigned));
49
50 Requirement(name, assign);
51 Requirement(input, assign);
52 Assignment(assign, output);
53 Succession(assign, assigned);
54
55 if (kind == VariableKind.Object)
56 {
57 Requirement(@object, assign);
58 }
59 }
60
61 private ControlOutput Assign(Flow flow)
62 {
63 var name = flow.GetValue<string>(this.name);
64 var input = flow.GetValue(this.input);
65
66 switch (kind)
67 {
68 case VariableKind.Flow:
69 flow.variables.Set(name, input);
70 break;
71 case VariableKind.Graph:
72 Variables.Graph(flow.stack).Set(name, input);
73 break;
74 case VariableKind.Object:
75 Variables.Object(flow.GetValue<GameObject>(@object)).Set(name, input);
76 break;
77 case VariableKind.Scene:
78 Variables.Scene(flow.stack.scene).Set(name, input);
79 break;
80 case VariableKind.Application:
81 Variables.Application.Set(name, input);
82 break;
83 case VariableKind.Saved:
84 Variables.Saved.Set(name, input);
85 break;
86 default:
87 throw new UnexpectedEnumValueException<VariableKind>(kind);
88 }
89
90 flow.SetValue(output, input);
91
92 return assigned;
93 }
94 }
95}