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.

I noticed people use both - but is there any empirical evidence when it's better to use one over the other?

This is related but not quite a 'why use either' or even 'why use both': Polling vs event driven input

share|improve this question
1  
Seems a little broad of a question, but generally the choice is based on the needs of the game/app. If the requirement is "when the X key/button/etc is down, fire thrusters", then polling. If the requirement is "when the X key/button/etc is pressed, toggle thrusters", then events. EDIT: text entry - always events. –  PlayDeezGames Oct 7 '13 at 13:50
1  
Like @PlayDeezGames said, it really depends on how your game is built. You'll know what type of input events you want for your game after doing a bit more research and/or development for you game. That's why this isn't a very good question for this site. –  TheNickmaster21 Oct 7 '13 at 14:35
add comment

3 Answers

Behind the scenes, typically input is polled anyway, just by the os behind your back ;)

Regardless, even if you poll you're going to need to package state changes up in some way in order to use that information elsewhere in your game. And odds on you're going to package that up in an event.

So realistically, going straight to events is going to save you the headache of state management and get you to where you need to go faster.

Be aware, that you dont want to be sending "button down!" or "user pressed A" events into your game, you want to re-interpret those using your keymapping and translate them into actual things like "the user jumped" or "start the game".

This will let you swap out controllers/input schemes/etc

share|improve this answer
 
"Behind the scenes, typically input is polled anyway, just by the os behind your back ;)" That is false. –  Adrian Maire Oct 8 '13 at 8:19
 
How else do they generate events? :) –  Matt D Oct 8 '13 at 23:47
 
Don't they use hardware interrupts @MattD? –  Vaughan Hilts Oct 9 '13 at 1:59
 
edwardbosworth.com/CPSC5155/PH4/Ch06/… This file explain basics I/O mechanisms. –  Adrian Maire Oct 9 '13 at 6:47
 
@AdrianMaire does HID use hardware interrupts? not many hardwired ps2 style input devices these days! –  Matt D Oct 9 '13 at 7:46
show 2 more comments

Event system is superior to Polling. The polling is a old technic that use innecesary CPU to test inputs. the event mechanism is better but more complex and require special hardware.

As all game platforms have support for events, you should use them always. You may transform the input state to a "state" variable value to test if you really require to poll it.

Events:

  • More complex (it require special hardware and concurrent programming/threading)
  • More accurated (The polling cycle may be slow, the event system is alway fast)
  • No extra CPU use

Polling:

  • Simple (it does not require multi-threading nor special hardware)
  • Used in most embedded system
  • Require extra CPU usage for polling, even if the input has not changed.
  • If the polling cycle is very fast (e.g. less 1ms), it may be faster than the event mechanism.
share|improve this answer
add comment

The issue of input events versus input polling is a matter of fidelity in most situations, and indeed, the reality is that one is simply a layer of abstraction built on top of another. Using input API's to directly read memory from a particular device gives you the option to control precisely how often you are reading input from a particular device, and in situations where you are attempting to simulate an analog/continuous function (driving/flying for instance) this is quite important. On the other hand, events tend to simulate discrete actions, and they work better for on/off type behaviors. Many systems use combinations of the two, based on their needs.

share|improve this answer
add comment

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.