Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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.

share|improve this question

1 Answer 1

up vote 1 down vote accepted

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.

share|improve this answer
    
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? –  Charliep123 Jun 15 at 2:01
    
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 –  dnedelchev Jun 15 at 12:12

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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