File size: 2,173 Bytes
18a519f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
using UnityObject = UnityEngine.Object;

namespace Unity.VisualScripting
{
    /// <summary>
    /// Branches flow depending on whether the input is null.
    /// </summary>
    [UnitCategory("Nulls")]
    [TypeIcon(typeof(Null))]
    public sealed class NullCheck : Unit
    {
        /// <summary>
        /// The input.
        /// </summary>
        [DoNotSerialize]
        [PortLabelHidden]
        public ValueInput input { get; private set; }

        /// <summary>
        /// The entry point for the null check.
        /// </summary>
        [DoNotSerialize]
        [PortLabelHidden]
        public ControlInput enter { get; private set; }

        /// <summary>
        /// The action to execute if the input is not null.
        /// </summary>
        [DoNotSerialize]
        [PortLabel("Not Null")]
        public ControlOutput ifNotNull { get; private set; }

        /// <summary>
        /// The action to execute if the input is null.
        /// </summary>
        [DoNotSerialize]
        [PortLabel("Null")]
        public ControlOutput ifNull { get; private set; }

        protected override void Definition()
        {
            enter = ControlInput(nameof(enter), Enter);
            input = ValueInput<object>(nameof(input)).AllowsNull();
            ifNotNull = ControlOutput(nameof(ifNotNull));
            ifNull = ControlOutput(nameof(ifNull));

            Requirement(input, enter);
            Succession(enter, ifNotNull);
            Succession(enter, ifNull);
        }

        public ControlOutput Enter(Flow flow)
        {
            var input = flow.GetValue(this.input);

            bool isNull;

            if (input is UnityObject)
            {
                // Required cast because of Unity's custom == operator.
                // ReSharper disable once ConditionIsAlwaysTrueOrFalse
                isNull = (UnityObject)input == null;
            }
            else
            {
                isNull = input == null;
            }

            if (isNull)
            {
                return ifNull;
            }
            else
            {
                return ifNotNull;
            }
        }
    }
}