Class DeferredCommand

java.lang.Object
edu.wpi.first.wpilibj2.command.Command
edu.wpi.first.wpilibj2.command.DeferredCommand
All Implemented Interfaces:
Sendable

public class DeferredCommand
extends Command
Defers Command construction to runtime. Runs the command returned by the supplier when this command is initialized, and ends when it ends. Useful for performing runtime tasks before creating a new command. If this command is interrupted, it will cancel the command.

Note that the supplier must create a new Command each call. For selecting one of a preallocated set of commands, use SelectCommand.

This class is provided by the NewCommands VendorDep

  • Constructor Details

    • DeferredCommand

      public DeferredCommand​(Supplier<Command> supplier, Set<Subsystem> requirements)
      Creates a new DeferredCommand that runs the supplied command when initialized, and ends when it ends. Useful for lazily creating commands at runtime. The Supplier will be called each time this command is initialized. The Supplier must create a new Command each call.
      Parameters:
      supplier - The command supplier
      requirements - The command requirements. This is a Set to prevent accidental omission of command requirements. Use Set.of() to easily construct a requirement set.
  • Method Details

    • initialize

      public void initialize()
      Description copied from class: Command
      The initial subroutine of a command. Called once when the command is initially scheduled.
      Overrides:
      initialize in class Command
    • execute

      public void execute()
      Description copied from class: Command
      The main body of a command. Called repeatedly while the command is scheduled.
      Overrides:
      execute in class Command
    • isFinished

      public boolean isFinished()
      Description copied from class: Command
      Whether the command has finished. Once a command finishes, the scheduler will call its end() method and un-schedule it.
      Overrides:
      isFinished in class Command
      Returns:
      whether the command has finished.
    • end

      public void end​(boolean interrupted)
      Description copied from class: Command
      The action to take when the command ends. Called when either the command finishes normally, or when it interrupted/canceled.

      Do not schedule commands here that share requirements with this command. Use Command.andThen(Command...) instead.

      Overrides:
      end in class Command
      Parameters:
      interrupted - whether the command was interrupted/canceled
    • initSendable

      public void initSendable​(SendableBuilder builder)
      Description copied from interface: Sendable
      Initializes this Sendable object.
      Specified by:
      initSendable in interface Sendable
      Overrides:
      initSendable in class Command
      Parameters:
      builder - sendable builder