Help using button.pressed()

So I havethis code:

bool arcade = true;

void tank (void) {
arcade = false;
}

void acrade(void) {
arcade = true;
}

Controller1.ButtonA.pressed(tank);
Controller1.ButtonB.pressed(acrade);

It works how its supposed to. Now my question is: is it possible to have it so when button a is pressed it goes to tank and when button a is pressed again it goes back to arcade? i dont want to have them on two seperate buttons if possible.

1 Like

Sure, then the callback (function that’s called when button A is pressed) would look something like:

void toggleArcade(void){
    if (arcade){
        arcade = false;
    }
    else{
        arcade = true;
    }
}

or a little more succinctly:

void toggleArcade(void){
    arcade = !arcade;
}

(Edit to add: if you were doing this sort of thing by polling the state of the controller buttons in an infinite loop, you’d have to worry about whether or not this is the first time through the loop with the button pressed, otherwise pressing the button will toggle between arcade and tank many times over. But the nice thing about using button::pressed here is that it takes care of that for you – your callback will be called only once each time the button is pressed.)

3 Likes

Thanks!
20 characters

1 Like