Decorators
Decorators
#4. Decorators
Decorators are called on any command to modify the way it works and return a new command from it, leaving the original unmodified.
Using decorators functionality is similar to the Group Commands.
| Decorator | Class behind it | Description |
|---|---|---|
withTimeout(ms) | TimeoutCommand | Acts like a timer, ends the command after ms. If the time cut it short, the inner command gets end(true). |
until(cond) | UntilCommand | Ends when the condition is true. The inner command gets end(true) if it had not finished on its own. |
andThen(cmds...) | SequentialCommandGroup | Runs this command, then the others in order. |
beforeStarting(cmds...) | SequentialCommandGroup | Runs the others first, then this command. |
andThenRun(Runnable) | InstantCommand | Shortcut for andThen(new InstantCommand(...)). |
afterDelay(ms) | DelayedCommand | Waits ms, then runs the command. |
alongWith(cmds...) | ParallelCommandGroup | Parallel; finishes when all finish. |
raceWith(cmds...) | ParallelRaceGroup | Parallel; finishes when any finishes. |
deadlineWith(cmds...) | ParallelDeadlineGroup | Parallel; finishes when this command finishes. |
repeatedly() | RepeatCommand | Repeats forever. |
repeat(n) | RepeatCommand | Repeats n times. |
perpetually() | PerpetualCommand | Ignores the inner isFinished(); runs until cancelled. |
finallyDo(Consumer<Boolean>) | FinallyCommand | Runs an action at the end, after the inner end(). The boolean says whether it was interrupted. |
onlyIf(cond) | ConditionalCommand | Runs only if the condition is true at start; otherwise finishes at once. |
unless(cond) | ConditionalCommand | The opposite of onlyIf. |
withRequirements(subs...) | AddRequirementsCommand | Adds subsystems to the command's requirements. |
asProxy() | ProxyCommand | See below. |
asProxy()ProxyCommand schedules the command on the scheduler (outside the group) and waits for it to finish. It does not inherit the command's requirements, so the group that contains it is not blocked by those subsystems. If the proxy is interrupted, it cancels the command it started. Use it when part of a sequence must outlive the group's other requirements.
#How they work
Decorators that wrap a single command (withTimeout, until, perpetually, finallyDo, withRequirements) extend WrapperCommand. It delegates initialize, execute, end, isFinished and getRequirements to the inner command, and each decorator overrides only what it changes. A wrapper's requirements are the inner command's, except withRequirements, which extends them.
#Beginner Shortcut
Just starting? A Routine writes the same sequence as a simple list of steps, with no classes to remember. See the Routine tab at the beginning of Commands.
Última actualización 2026-11-16