File size: 1,704 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
namespace Unity.VisualScripting
{
    /// <summary>
    /// Branches flow by checking if a condition is true or false.
    /// </summary>
    [UnitCategory("Control")]
    [UnitOrder(0)]
    [RenamedFrom("Bolt.Branch")]
    [RenamedFrom("Unity.VisualScripting.Branch")]
    public sealed class If : Unit, IBranchUnit
    {
        /// <summary>
        /// The entry point for the branch.
        /// </summary>
        [DoNotSerialize]
        [PortLabelHidden]
        public ControlInput enter { get; private set; }

        /// <summary>
        /// The condition to check.
        /// </summary>
        [DoNotSerialize]
        [PortLabelHidden]
        public ValueInput condition { get; private set; }

        /// <summary>
        /// The action to execute if the condition is true.
        /// </summary>
        [DoNotSerialize]
        [PortLabel("True")]
        public ControlOutput ifTrue { get; private set; }

        /// <summary>
        /// The action to execute if the condition is false.
        /// </summary>
        [DoNotSerialize]
        [PortLabel("False")]
        public ControlOutput ifFalse { get; private set; }

        protected override void Definition()
        {
            enter = ControlInput(nameof(enter), Enter);
            condition = ValueInput<bool>(nameof(condition));
            ifTrue = ControlOutput(nameof(ifTrue));
            ifFalse = ControlOutput(nameof(ifFalse));

            Requirement(condition, enter);
            Succession(enter, ifTrue);
            Succession(enter, ifFalse);
        }

        public ControlOutput Enter(Flow flow)
        {
            return flow.GetValue<bool>(condition) ? ifTrue : ifFalse;
        }
    }
}