0
\$\begingroup\$

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.

\$\endgroup\$
3
  • \$\begingroup\$ Can the execution of one command change how you map inputs to commands for subsequent inputs? Or is it safe to accumulate all your commands for the frame into a queue and then iterate over that queue? \$\endgroup\$ Commented Sep 25, 2020 at 17:36
  • \$\begingroup\$ @DMGregory I think it's safe to assume that the the mapping won't change. Using a queue could probably solve my problem. I'm trying to think of the best way to implement that. Assuming that I have user inputs and an AI system that both produce commands. \$\endgroup\$ Commented Sep 25, 2020 at 17:58
  • 1
    \$\begingroup\$ "Using a queue could probably solve my problem." Yeah, that. \$\endgroup\$ Commented Sep 25, 2020 at 22:44

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.