A game about forced loneliness, made by TACStudios
1namespace Unity.VisualScripting
2{
3 /// <summary>
4 /// Selects a value from a set by checking if a condition is true or false.
5 /// </summary>
6 [UnitCategory("Control")]
7 [UnitTitle("Select")]
8 [TypeIcon(typeof(ISelectUnit))]
9 [UnitOrder(6)]
10 public sealed class SelectUnit : Unit, ISelectUnit
11 {
12 /// <summary>
13 /// The condition to check.
14 /// </summary>
15 [DoNotSerialize]
16 [PortLabelHidden]
17 public ValueInput condition { get; private set; }
18
19 /// <summary>
20 /// The value to return if the condition is true.
21 /// </summary>
22 [DoNotSerialize]
23 [PortLabel("True")]
24 public ValueInput ifTrue { get; private set; }
25
26 /// <summary>
27 /// The value to return if the condition is false.
28 /// </summary>
29 [DoNotSerialize]
30 [PortLabel("False")]
31 public ValueInput ifFalse { get; private set; }
32
33 /// <summary>
34 /// The returned value.
35 /// </summary>
36 [DoNotSerialize]
37 [PortLabelHidden]
38 public ValueOutput selection { get; private set; }
39
40 protected override void Definition()
41 {
42 condition = ValueInput<bool>(nameof(condition));
43 ifTrue = ValueInput<object>(nameof(ifTrue)).AllowsNull();
44 ifFalse = ValueInput<object>(nameof(ifFalse)).AllowsNull();
45 selection = ValueOutput(nameof(selection), Branch).Predictable();
46
47 Requirement(condition, selection);
48 Requirement(ifTrue, selection);
49 Requirement(ifFalse, selection);
50 }
51
52 public object Branch(Flow flow)
53 {
54 return flow.GetValue(flow.GetValue<bool>(condition) ? ifTrue : ifFalse);
55 }
56 }
57}