namespace Unity.VisualScripting { /// /// Executes an action only once, and a different action afterwards. /// [UnitCategory("Control")] [UnitOrder(14)] public sealed class Once : Unit, IGraphElementWithData { public sealed class Data : IGraphElementData { public bool executed; } /// /// The entry point for the action. /// [DoNotSerialize] [PortLabelHidden] public ControlInput enter { get; private set; } /// /// Trigger to reset the once check. /// [DoNotSerialize] public ControlInput reset { get; private set; } /// /// The action to execute the first time the node is entered. /// [DoNotSerialize] public ControlOutput once { get; private set; } /// /// The action to execute subsequently. /// [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(this); if (!data.executed) { data.executed = true; return once; } else { return after; } } public ControlOutput Reset(Flow flow) { flow.stack.GetElementData(this).executed = false; return null; } } }