I am making a program that reads input from an xbox controller and I have some boolean variables in the code that need to be toggled. I have this template for my function.
void toggle(bool condition, bool &variable) // Used to toggle variables
{
}
The condition parameter will be a button on the controller (ex. buttonA) and the variable parameter is a reference to the variable that needs to be toggled (ex. crouching)
The problem is that since the controller input is read constantly, a player cannot press and release the button fast enough that it is not read more than once, which causes the variable I am trying to toggle to just rapidly switch back and forth. The behavior I want is that when A is pressed, the value of crouching changes to the opposite of what it is (crouching = !crouching) but I only want it to be run once when the button is pressed and wait for it to be released before the function can be called again. I have tried but I cannot figure out an algorithm to do this. Any help is appreciated!