1
\$\begingroup\$

In our game created with c++ and sdl we are having difficulty delegating how the main character should be controlled. We have a keyboard class and a sprite class for the main character, but are unsure if the main character should have an instance of the keyboard class or if keyboard input should be done through the game loop. If you need anymore information I would be happy to provide it, thanks in advance for any help.

\$\endgroup\$
1
\$\begingroup\$

Your main character class should not have a reference to the keyboard or any input-related class. Input should be handled in the game loop, or preferably a dedicated input class, decoupled from other entities so they'll only need to respond to abstract actions, not raw input.

Not all input is related to your characters and and not all types of input devices are the same, so your best bet is to separate the input handling into several stages so you can handle it contextually e.g.

  1. Gather input data
  2. Examine current game state or context (MainMenu, InGame, InGameMenu, GameOver etc.)
  3. Mapping raw input to actions ... etc.

This will make it easier for you to implement new actions, control schemes, input devices or multiplayer controls.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ How does the transition from the input class to the main character work? Is the data sent from the input class to the main character in the game loop? \$\endgroup\$ – Charliep123 Jun 15 '15 at 2:01
  • \$\begingroup\$ Depends on you architecture. Yes, the simplest way would be to listen for input in the game loop and map it with something like map<Key, Action>. For more elaborate designs you can take a look at these: gamedev.stackexchange.com/questions/59582/… , gameprogrammingpatterns.com/command.html \$\endgroup\$ – dnedelchev Jun 15 '15 at 12:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.