A game about forced loneliness, made by TACStudios
1using UnityObject = UnityEngine.Object;
2
3namespace Unity.VisualScripting
4{
5 /// <summary>
6 /// Provides a fallback value if the input value is null.
7 /// </summary>
8 [UnitCategory("Nulls")]
9 [TypeIcon(typeof(Null))]
10 public sealed class NullCoalesce : Unit
11 {
12 /// <summary>
13 /// The value.
14 /// </summary>
15 [DoNotSerialize]
16 public ValueInput input { get; private set; }
17
18 /// <summary>
19 /// The fallback to use if the value is null.
20 /// </summary>
21 [DoNotSerialize]
22 public ValueInput fallback { get; private set; }
23
24 /// <summary>
25 /// The returned value.
26 /// </summary>
27 [DoNotSerialize]
28 [PortLabelHidden]
29 public ValueOutput result { get; private set; }
30
31 protected override void Definition()
32 {
33 input = ValueInput<object>(nameof(input)).AllowsNull();
34 fallback = ValueInput<object>(nameof(fallback));
35 result = ValueOutput(nameof(result), Coalesce).Predictable();
36
37 Requirement(input, result);
38 Requirement(fallback, result);
39 }
40
41 public object Coalesce(Flow flow)
42 {
43 var input = flow.GetValue(this.input);
44
45 bool isNull;
46
47 if (input is UnityObject)
48 {
49 // Required cast because of Unity's custom == operator.
50 // ReSharper disable once ConditionIsAlwaysTrueOrFalse
51 isNull = (UnityObject)input == null;
52 }
53 else
54 {
55 isNull = input == null;
56 }
57
58 return isNull ? flow.GetValue(fallback) : input;
59 }
60 }
61}