SFML community forums
Help => General => Topic started by: freesoul on August 13, 2013, 07:37:58 pm
-
What is better way to get input of mouse/keyboard for a game? Events, or testing global input?
-
Please read the tutorials, they explain exactly this kind of stuff :)
-
In which tutorial can I find which system is better? I could read what does events and what does global input, but I didn't find which I should use
-
None is "better". They have different use cases.
Read:
http://www.sfml-dev.org/tutorials/2.1/window-events.php
http://www.sfml-dev.org/tutorials/2.1/window-inputs.php
If you understand what they're doing, then you'll know which one to use where. Besides, it's also explicitly mentioned.
-
Although those documentations does not explicitly explain why one would choose one over the other. It doesn't explain why you'd want to query the state of a keyboard as opposed to using events or vice-versa. Admittedly that's a wider discussion in "game coding" but none-the-less, I can see why it'd be a legitimate question what situations one is better than the over.
Heck, I'd say
Sometimes, people try to use the KeyPressed event to implement smooth movement. Doing so will not produce smooth movements, because when you hold a key you only get a few events (remember, the repeat delay). Smooth movements can only be achieved by using real-time keyboard input with sf::Keyboard (see the dedicated tutorial).
is downright misleading. Smooth movement is perfectly possible with events, it's just the way you go about it is different. Not "Can I move? If so, move" but "Apply velocity on press, Remove velocity on release".
-
I would say, the first paragraph is very clear regarding the different use cases:
This tutorial explains how to access global inputs: keyboard, mouse and joysticks. This must not be confused with events: real-time inputs allow you to query the global state of keyboard, mouse and joysticks at any time ("is this button currently pressed?", "where is the mouse?") while events notify you when something happens ("this button was pressed", "the mouse has moved").
You're right that one cannot know when to use which for a specific game, but some concrete application scenarios are then listed in the respective tutorials.
However I agree that the sentence concerning smooth movements should be clarified.
-
I'd say it's explaining the difference in functionality. It's saying how real-time says how things are, and events says when things change. Those tutorials are very good at explaining the difference in functionality, and that's a good thing.
What they aren't is game code design tutorials or discussions on approaches to structuring game code and I believe this is more that freesoul here is asking about: To know the high level design reasons one would choose one over the other. And that's a more complex discussion with a lot more nuance than those tutorials allow/need.