# Handling inputs the right way

This topic is 2121 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi,
I'm (ultra) confuse about input managment. I've the global idea, but I can figure out how to implement something which fit my design (maybe because it's a lame one).
The context is a state based game. For the example, we wil define 3 states : MAIN_MENU, GAME_RUNNING, and GAME_CASTING_SPELL.
In this 3 states, the action "left-button mouse clic" will be handled differently :
- In the MAIN_MENU state, the input will change the state, from MAIN_MENU to GAME_RUNNING.
- In the GAME_RUNNING state, the input triggers a player action : cast a spell (let's say firebolt), which change the state from GAME_RUNNING to CASTING_SPELL.
- In the CASTING_SPELL state, the input gives informations for the spell, for example his target. You clic somewhere and the firebolt goes there. This input changes the state from CASTING_SPELL to GAME_RUNNING.
Somewhere I'll need a class (let's call it InputManager) which needs to know the state the game currently is, like this (pseudo-code):
 (method called every tick update(GameState state) { switch(state) { case MAIN_MENU : if(mouse_leftbutton_clic) *go to GAME state* break; case GAME_RUNNING : if(mouse_leftbutton_clic) *go to CASTING_SPELL state* break; case CASTING_SPELL : if(mouse_leftbutton_clic) *launch firebolt and go to GAME_RUNNING state* break; } } 
How do i link my classes together ? My input manager needs to know the existence of the other objects and classes to update their logic.
Plus, what happens when I want an event to trigger when the player presses 2 buttons at the same time ?
Should my input manager return an array defining the state of my keyboard and mouse input and instant T ?
What I said may be huge crap, I need a teacher
Thanks !

##### Share on other sites
Something I've used in the past was having something like
[source lang="java"]interface State {
void processInput(InputEvent event);
State getNextState();
}[/source]

The gameloop then forwards all events it doesn't handle directly to the currently active state and at the end of the game loop queries the next state and sets that as active. That avoids this global switch construct which will get out of hand and unmaintainable pretty quickly.

##### Share on other sites
Alright, that sounds like a good idea.
Does your input manager (let's say you have one for each state) needs to know your logic (so you have to pass it references of your logic objects so he can manipulate them) or does it simply return some infos on what input have been triggered, infos that the logic is able to decode and modify itself ?
I would say the second one, since your input manager shouldn't be doing something else than managing inputs.

1. 1
Rutin
26
2. 2
3. 3
JoeJ
20
4. 4
5. 5

• 10
• 10
• 9
• 9
• 10
• ### Forum Statistics

• Total Topics
631752
• Total Posts
3002091
×