Jump to content

  • Log In with Google      Sign In   
  • Create Account

We need your help!

We need 1 more developer from Canada and 12 more from Australia to help us complete a research survey.

Support our site by taking a quick sponsored survey and win a chance at a $50 Amazon gift card. Click here to get started!

Handling inputs the right way

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 Firnael   Members   -  Reputation: 182


Posted 26 September 2012 - 04:14 AM

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 <img src='http://public.gamedev.net//public/style_emoticons/<#EMO_DIR#>/smile.png' class='bbc_emoticon' alt=':)' />
update(GameState state) {
	switch(state) {
		case MAIN_MENU :
			*go to GAME state*
	   case GAME_RUNNING :
			*go to CASTING_SPELL state*
			*launch firebolt and go to GAME_RUNNING state*
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 Posted Image
Thanks !


#2 japro   Members   -  Reputation: 887


Posted 26 September 2012 - 04:26 AM

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.

#3 Firnael   Members   -  Reputation: 182


Posted 27 September 2012 - 01:48 AM

Alright, that sounds like a good idea.
Now how do you update your logic with your inputs ?
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.

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.