File size: 2,060 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
namespace Unity.VisualScripting
{
    /// <summary>
    /// Executes an action only once, and a different action afterwards.
    /// </summary>
    [UnitCategory("Control")]
    [UnitOrder(14)]
    public sealed class Once : Unit, IGraphElementWithData
    {
        public sealed class Data : IGraphElementData
        {
            public bool executed;
        }

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

        /// <summary>
        /// Trigger to reset the once check.
        /// </summary>
        [DoNotSerialize]
        public ControlInput reset { get; private set; }

        /// <summary>
        /// The action to execute the first time the node is entered.
        /// </summary>
        [DoNotSerialize]
        public ControlOutput once { get; private set; }

        /// <summary>
        /// The action to execute subsequently.
        /// </summary>
        [DoNotSerialize]
        public ControlOutput after { get; private set; }

        protected override void Definition()
        {
            enter = ControlInput(nameof(enter), Enter);
            reset = ControlInput(nameof(reset), Reset);
            once = ControlOutput(nameof(once));
            after = ControlOutput(nameof(after));

            Succession(enter, once);
            Succession(enter, after);
        }

        public IGraphElementData CreateData()
        {
            return new Data();
        }

        public ControlOutput Enter(Flow flow)
        {
            var data = flow.stack.GetElementData<Data>(this);

            if (!data.executed)
            {
                data.executed = true;

                return once;
            }
            else
            {
                return after;
            }
        }

        public ControlOutput Reset(Flow flow)
        {
            flow.stack.GetElementData<Data>(this).executed = false;

            return null;
        }
    }
}