Add Input `is_anything_pressed` method #35012
Conversation
if (E->get().pressed) { | ||
return true; | ||
} | ||
} |
RebelliousX
Jan 19, 2020
Contributor
The code is good, but I have a small suggestion! Instead of checking if each key is pressed which can harm performance, why not check to see if any key was pressed using only 1 if statement! This can be easily done by creating an integer counter before the for loop and initialized to 0, then instead of doing if (E->get().pressed)
we do counter += (int)E->get().pressed
.. after the for loop, if the counter is over 0, then a key was pressed.
The code is good, but I have a small suggestion! Instead of checking if each key is pressed which can harm performance, why not check to see if any key was pressed using only 1 if statement! This can be easily done by creating an integer counter before the for loop and initialized to 0, then instead of doing if (E->get().pressed)
we do counter += (int)E->get().pressed
.. after the for loop, if the counter is over 0, then a key was pressed.
Xrayez
Jan 19, 2020
Author
Member
I just profiled both current and your suggestion, I couldn't detect much performance difference (at most 1 usec improvement). That's around going through 5-8 actions. With the counter, you do have to go through all actions. But when you detect if the action is pressed, you can just exit the loop with return
immediately. Most of the time the user would actually be pressing something, so we're interested in short-cutting I think. If the user is away, the performance doesn't matter anyway. So, readability would be preferred in this case imo.
I'll try to see if there's any more significant difference with more actions, but I doubt the user would like to define many of them. In fact, it's not going through all InputMap
actions, but only those which were pressed during gameplay. It means that the more actions you press, the more the action_state
would look like an InputMap
.
I just profiled both current and your suggestion, I couldn't detect much performance difference (at most 1 usec improvement). That's around going through 5-8 actions. With the counter, you do have to go through all actions. But when you detect if the action is pressed, you can just exit the loop with return
immediately. Most of the time the user would actually be pressing something, so we're interested in short-cutting I think. If the user is away, the performance doesn't matter anyway. So, readability would be preferred in this case imo.
I'll try to see if there's any more significant difference with more actions, but I doubt the user would like to define many of them. In fact, it's not going through all InputMap
actions, but only those which were pressed during gameplay. It means that the more actions you press, the more the action_state
would look like an InputMap
.
Your typical "Press any key" use case using the🙂
Input
singleton, can also be used to conveniently detect and switch variousidle
states, such as character switching tobored
state if you wish so.There's another way for doing so using
_input
:But it might not be possible to achieve in the future according to:
godot/core/os/input_event.h
Lines 191 to 194 in 02cd144
In any case, I'm here to tell that it might be a good idea to leave
InputEvent.is_pressed()
as is.Test project
press-any-key.zip