Help > General

isKeyPressed() vs event checking

(1/2) > >>

BeautiCode:
Which do you guys think is better? Checking to see if the user has pressed a key in your event loop, or just calling isKeyPressed() in your main loop? Are there any performance benefits to either, besides being able to check if 2 keys are pressed at once?

BaneTrapper:

--- Quote from: BeautiCode on August 16, 2014, 10:04:31 pm ---Which do you guys think is better? Checking to see if the user has pressed a key in your event loop, or just calling isKeyPressed() in your main loop? Are there any performance benefits to either, besides being able to check if 2 keys are pressed at once?

--- End quote ---
This belongs in discussion not help.EDIT:: :o
Its all explain in the tutorials and documentation, i suggest reading those.
BTW each one has its own benefits and downfalls.

Ixrec:
It's not about performance.  Real time input (isKeyPressed()) and event-based input have very different semantics, so in most cases one of the two is "correct" and the other is "incorrect".  Like BaneTrapper said, the tutorials explain this, but many people seem to benefit from having these things explained multiple times by different people (at least I hope that's why we keep getting asked questions the tutorials clearly answer already).

Events are correct when you want to "react" to something the user did and you don't care when it happened.  For instance, the sf::Close event is something you want to react to, and you don't really care how long ago the user clicked the X button or hit alt+F4 or whatever; you still want to close the window.  Things like text fields in GUIs should use events.  When you care about changes in state (ie, the key went from unpressed to pressed) you probably want events.

Real-time input is correct when you need to know what the user is doing at this particular moment in time.  Any time holding down a button is different from quickly pressing it multiple times, you need real-time input.  For instance, if an enemy hits the player, you need to know if the player is holding the guard button at this moment in time.  Most gameplay should use real-time input.  When you care about the state at the current moment (ie, is the key pressed right now) you probably want real-time input.

Hopefully that's enough to clarify it.

By the way, both methods can handle multiple key presses.  In the event case, you just get one event for each press.  You're supposed to loop through all available events every frame (like the SFML tutorials do).

eXpl0it3r:

--- Quote from: BaneTrapper on August 16, 2014, 10:08:07 pm ---This belongs in discussion not help.

--- End quote ---
No! The "General Discussion" is about general stuff. BeautiCode is looking for help on his question and thus it belongs to the Help section. Please don't make such suggestions if you don't fully understand how Laurent has setup the sub-forums. ;)

As for the topic: It largely don't matter. While totally not important for your basic game, isKeyPressed() has a small performance cost on Linux, due to the X11 code bits. Might be changed in the future, thus as I said, it shouldn't concern you. :D

BaneTrapper:

--- Quote from: eXpl0it3r on August 16, 2014, 11:25:01 pm ---
--- Quote from: BaneTrapper on August 16, 2014, 10:08:07 pm ---This belongs in discussion not help.

--- End quote ---
No! The "General Discussion" is about general stuff. BeautiCode is looking for help on his question and thus it belongs to the Help section. Please don't make such suggestions if you don't fully understand how Laurent has setup the sub-forums. ;)

--- End quote ---
I strictly believe he asked for discussion with this:

--- Quote from: BeautiCode on August 16, 2014, 10:04:31 pm ---Which do you guys think is better? Checking to see if the user has pressed a key in your event loop, or just calling isKeyPressed() in your main loop?

--- End quote ---
But the other part is a question, which i know too little to be called an expert, so i wouldn't miss inform him.

--- Quote from: BeautiCode on August 16, 2014, 10:04:31 pm ---Are there any performance benefits to either, besides being able to check if 2 keys are pressed at once?

--- End quote ---
From http://en.sfml-dev.org/forums/index.php?topic=5559.0"Read before posting"
I quote Laurent: "A lot of people end up posting in the wrong forum, and this can be irritating for admins. Moreover, people may not read your post if it's at the wrong place".
I was hasty, i analysed the post incorrectly and assumed there is no question  :'( i am sorry BeautiCode!

Navigation

[0] Message Index

[#] Next page

Go to full version