A game about forced loneliness, made by TACStudios
1using UnityEngine;
2
3namespace Unity.VisualScripting
4{
5 /// <summary>
6 /// Gets the value of a variable.
7 /// </summary>
8 public sealed class GetVariable : UnifiedVariableUnit
9 {
10 /// <summary>
11 /// The value of the variable.
12 /// </summary>
13 [DoNotSerialize]
14 [PortLabelHidden]
15 public ValueOutput value { get; private set; }
16
17 /// <summary>
18 /// The value to return if the variable is not defined.
19 /// </summary>
20 [DoNotSerialize]
21 public ValueInput fallback { get; private set; }
22
23 /// <summary>
24 /// Whether a fallback value should be provided if the
25 /// variable is not defined.
26 /// </summary>
27 [Serialize]
28 [Inspectable]
29 [InspectorLabel("Fallback")]
30 public bool specifyFallback { get; set; } = false;
31
32 protected override void Definition()
33 {
34 base.Definition();
35
36 value = ValueOutput(nameof(value), Get).PredictableIf(IsDefined);
37
38 Requirement(name, value);
39
40 if (kind == VariableKind.Object)
41 {
42 Requirement(@object, value);
43 }
44
45 if (specifyFallback)
46 {
47 fallback = ValueInput<object>(nameof(fallback));
48 Requirement(fallback, value);
49 }
50 }
51
52 private bool IsDefined(Flow flow)
53 {
54 var name = flow.GetValue<string>(this.name);
55
56 if (string.IsNullOrEmpty(name))
57 {
58 return false;
59 }
60
61 GameObject @object = null;
62
63 if (kind == VariableKind.Object)
64 {
65 @object = flow.GetValue<GameObject>(this.@object);
66
67 if (@object == null)
68 {
69 return false;
70 }
71 }
72
73 var scene = flow.stack.scene;
74
75 if (kind == VariableKind.Scene)
76 {
77 if (scene == null || !scene.Value.IsValid() || !scene.Value.isLoaded || !Variables.ExistInScene(scene))
78 {
79 return false;
80 }
81 }
82
83 switch (kind)
84 {
85 case VariableKind.Flow:
86 return flow.variables.IsDefined(name);
87 case VariableKind.Graph:
88 return Variables.Graph(flow.stack).IsDefined(name);
89 case VariableKind.Object:
90 return Variables.Object(@object).IsDefined(name);
91 case VariableKind.Scene:
92 return Variables.Scene(scene.Value).IsDefined(name);
93 case VariableKind.Application:
94 return Variables.Application.IsDefined(name);
95 case VariableKind.Saved:
96 return Variables.Saved.IsDefined(name);
97 default:
98 throw new UnexpectedEnumValueException<VariableKind>(kind);
99 }
100 }
101
102 private object Get(Flow flow)
103 {
104 var name = flow.GetValue<string>(this.name);
105
106 VariableDeclarations variables;
107
108 switch (kind)
109 {
110 case VariableKind.Flow:
111 variables = flow.variables;
112 break;
113 case VariableKind.Graph:
114 variables = Variables.Graph(flow.stack);
115 break;
116 case VariableKind.Object:
117 variables = Variables.Object(flow.GetValue<GameObject>(@object));
118 break;
119 case VariableKind.Scene:
120 variables = Variables.Scene(flow.stack.scene);
121 break;
122 case VariableKind.Application:
123 variables = Variables.Application;
124 break;
125 case VariableKind.Saved:
126 variables = Variables.Saved;
127 break;
128 default:
129 throw new UnexpectedEnumValueException<VariableKind>(kind);
130 }
131
132 if (specifyFallback && !variables.IsDefined(name))
133 {
134 return flow.GetValue(fallback);
135 }
136
137 return variables.Get(name);
138 }
139 }
140}