Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Player input - Should it be handled in RenderWindow events or custom code?  (Read 3353 times)

0 Members and 1 Guest are viewing this topic.

ggggggggggggggg

  • Newbie
  • *
  • Posts: 36
    • View Profile
    • Email
So I was looking over a friend's project, which was a platformer, and he was using input from the RenderWindow itself. And, if haven't already guessed, it didn't work well. I'm not sure what it's called, but when you use input from a window event it has that little pause between the initial invoke and then reoccurring invoke (not sure the exact the words to describe that...) But this can be averted by using an external variable.

So my question is this: would it be best to work around that problem using an external variable? Or is the RenderWindow events best used for things like control/UI? If using an external variable, wouldn't you basically have to write code twice? Once to set the current velocity (KeyPressed) and stop the input velocity (KeyReleased).
« Last Edit: March 13, 2016, 06:27:54 am by asusralis »

DarkRoku12

  • Full Member
  • ***
  • Posts: 203
  • Lua coder.
    • View Profile
    • Email
If you need the real state at real time of an input:

See this tutorial section

And if you need to know the "holding time" , you can combine sf::clock (or <chrono> ) and sf::(InputDevice)::is(eventType)
« Last Edit: March 13, 2016, 06:48:01 am by DarkRoku »
I would like a spanish/latin community...
Problems building for Android? Look here

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11033
    • View Profile
    • development blog
    • Email
Using events is perfectly fine. However if you want to do some action while the key is held, you shouldn't just check the KeyPressed event and wait for the key repeat to kick in. Instead you'd track the state with some boolean and for repeated actions use a clock. I'd suggest to take a look at Thor's action class.

sf::Keyboard/sf::Mouse are also an option.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/