Class StateMachine.TransitionNeedsConditionStage

java.lang.Object
org.wpilib.command3.StateMachine.TransitionNeedsConditionStage
Enclosing class:
StateMachine

public static final class StateMachine.TransitionNeedsConditionStage extends Object
A builder to set conditions for a transition from one state to another. Use when(BooleanSupplier) to make the transition occur when some external condition becomes true, or use whenComplete() to make the transition occur when the originating state completes without having reached any other transitions first.
  • Method Details

    • when

      public void when(BooleanSupplier condition)
      Adds a transition that will be triggered when the specified condition becomes true.

      NOTE: this had no effect if the originating state is a one-shot command without a yield. Use whenComplete() instead for transitions from one-shot commands.

      If multiple transitions are triggered in the same scheduler loop iteration, the first transition will fire and the rest will be ignored.

      StateMachine stateMachine = new StateMachine("Example State Machine");
      State state1 = stateMachine.addState(...);
      State state2 = stateMachine.addState(...);
      State state3 = stateMachine.addState(...);
      
      state1.switchTo(state2).when(() -> foo == true);
      
      // never triggers because the first transition will be evaluated first
      state1.switchTo(state3).when(() -> foo == true);
      
      Parameters:
      condition - The condition that will trigger the transition. Cannot be null.
    • whenComplete

      public void whenComplete()
      Adds a transition to the target state when the originating state completes without having triggered any other transitions first. If this is called multiple times for the same originating state, later calls will override the previous transitions. Any whenCompleteAnd(BooleanSupplier) transitions will take precedence over whenComplete transitions if their conditions are met when the state exits.
      StateMachine stateMachine = new StateMachine("Example State Machine");
      State state1 = stateMachine.addState(...);
      State state2 = stateMachine.addState(...);
      State state3 = stateMachine.addState(...);
      
      state1.switchTo(state2).whenComplete();
      state1.switchTo(state3).whenComplete(); // Overrides the previous transition
      state1.exitStateMachine().whenCompleteAnd(...); // Takes precedence if the condition is met
      
    • whenCompleteAnd

      public void whenCompleteAnd(BooleanSupplier condition)
      Similar to when(BooleanSupplier), but only triggers when the originating state completes and some other condition is also met. whenCompleteAnd transitions will be evaluated in declaration order and take precedence over any whenComplete() transitions that have been specified.
      StateMachine stateMachine = new StateMachine("Example State Machine");
      State state1 = stateMachine.addState(...);
      State state2 = stateMachine.addState(...);
      State state3 = stateMachine.addState(...);
      
      state1.switchTo(state2).whenComplete();
      state1.switchTo(state3).whenComplete(); // Overrides the previous transition
      state1.exitStateMachine().whenCompleteAnd(...); // Takes precedence if the condition is met
      
      Parameters:
      condition - The condition that will trigger the transition.