I'm reading Game Programming Patterns by Robert Nystrom. As a test I've implemented the command pattern, but a little farther in he changes it to return commands, rather than instantly executing them.
An example of the first command pattern:
void InputHandler::handleInput()
{
if (isPressed(BUTTON_X)) buttonX_->execute();
else if (isPressed(BUTTON_Y)) buttonY_->execute();
else if (isPressed(BUTTON_A)) buttonA_->execute();
else if (isPressed(BUTTON_B)) buttonB_->execute();
}
Changing it to return commands:
Command* InputHandler::handleInput()
{
if (isPressed(BUTTON_X)) return buttonX_;
if (isPressed(BUTTON_Y)) return buttonY_;
if (isPressed(BUTTON_A)) return buttonA_;
if (isPressed(BUTTON_B)) return buttonB_;
// Nothing pressed, so do nothing.
return NULL;
}
// Executed in game loop:
Command* command = inputHandler.handleInput();
if (command)
{
command->execute(actor);
}
My question is, how would I go about processing multiple user inputs each time input is handled? In my sample implementation, I check with an if on each button, instead of how he does it using an else if. I can see the benefits of returning the command (execute the command on any game actor you send to it), but it appears to be limited to one command process per loop.