View Single Post
  #7   Spotlight this post!  
Unread 23-02-2013, 14:13
Itamar's Avatar
Itamar Itamar is offline
Registered User
FRC #1943 (NeatTeam) & FRC #4590 (GreenBlitz)
Team Role: Mentor
 
Join Date: Jan 2010
Rookie Year: 2010
Location: Israel
Posts: 83
Itamar is an unknown quantity at this point
Re: Command-Based robot button toggle question

Quote:
Originally Posted by Fifthparallel View Post
It lies in the "new" keyword. The command you're instantiating is not a singleton, nor is it atomic. On that note, the WPILib function WhenPressed for a Joystick Button takes an argument for a command. You can insert new to make it create a new instance of that Command, i.e., the constructor for a new Command will run every time that button is pressed.
Uh... no, sorry, that's not how it works. If you look at the documentation, you'll see the following line:

Code:
void WhenActive (Command *command)
That means the function receives a pointer pointing at a Command object. When you write:

Code:
new DoStuff()
The type you get from that expression is a pointer to a DoStuff object (the one you just created). If you can point me to any piece of code that suggests it somehow uses that object to create more objects of that kind, please do.

Looking deeper into the code, you can see that instance ends up being stored in an instance of "PressedButtonScheduler", which takes it upon itself to check the state of the button and start the command instance if necessary (not create another instance).
Reply With Quote