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
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It only takes a minute to sign up.
Sign up to join this communityI 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
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
Polling mechanism
The software check for a condition repeated times until something is ready.
E.g. Childs in a car:
Child: Did we arrived yet?
Mom: No;
Child: Did we arrived yet?
Mom: No;
.... (repeat many times)
Child: Did we arrived yet?
Mom: Yes
Events
The condition itself is able to inform the software when it is ready.
E.g. Childs in a car:
Mom: I will tell you when we arrive
Child: Ok, I am going to read in the while
... (child reading silently)
Mom: We arrived!
Child: Great!
Events mechanism is superior to polling: due to several reasons:
There are a few benefits for the poll mechanism:
Summary: As all game platforms have support for events, you should use them always.
Why to use both
I do not know about any real situation in which polling is recommended in game development.
Obviously, polling is extremely important in other fields, for example signal sampling.
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.