Quick question on I guess how commands work. Say I have these two scenarios in RobotContainer:
//Inside the Field
private final moveArmCommand moveArm_90 = new moveArmCommand(90, m_arm)
.
.
.
//In the Constructor of RobotContainer/configureButtonBindings()
new JoystickButton(controller, 1)
.onTrue(moveArm_90);
Versus
2.
//In the Constructor of RobotContainer/configureButtonBindings()
new JoystickButton(controller, 1)
.onTrue(new moveArmCommand(90, m_arm));
Which one is correct?
Do we want to instantiate a single instance of a command and then every time we press a button it will get scheduled? Does that mean we are just resuming the same command if we hit the button again in the future? Does that mess anything up???
Is the 2nd one preferred. Should we create a new instance of the command each button press? Will that bog anything down. Does that new instance of the command replace any previous on when it’s scheduled?
I’m afraid making one instance of a command in the field of robotcontainer is bad because it might resume at a weird state if the command never finished, but I’m also afraid that it’s not good to keep on making new instances of a command on every button press. Someone help me sure up my thinking please!
Both options do the same thing. If you want to make a new command every time, you’ll have to use the Proxy command that takes in a Supplier<Command>. button.onTrue(new ProxyCommand(() -> new moveArmCommand(90, m_arm)));
What you have works just fine in 95-99% of cases. We tend to use the second option because it reduces line counts and is more readable IMO. Where you would potentially want a proxy command is if you’re trying to change some value that is being passed into the command. If that 90 was instead some variable that you want to change the value of throughout a match, both your options would keep the initial value and the command would never change, but with the proxy, a new command is generated every time the button is called.
Thanks! Just so I understand how Commands work. Every time I press a button in both scenarios I am just re-scheduling the existing command? Every time a command is scheduled it starts new (does it’s initialize, execute, etc.) Is that correct?
The Command’s constructor is executed once when the program flow passes through the new command object statement. You are exactly right that the command’s methods init, execute, etc are all performed when the command is scheduled.