-
-
Notifications
You must be signed in to change notification settings - Fork 21.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Input is_anything_pressed
method
#35012
Conversation
main/input_default.cpp
Outdated
if (E->get().pressed) { | ||
return true; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
.
This PR is one year old, yes or no? It would be bad to close it just like that as this PR received some good chunk of 👍s. |
I've been thinking about practical use cases for this, but I'm having trouble finding any.
This can generally be achieved by checking whether any of the movement or action keys have been pressed in the last N seconds. As for "Press any key to continue" dialogs, this can be achieved using |
To clarify your post, there are use cases for this (it's quoted), there are just existing alternatives which may or may not make this PR less useful in comparison.
It's the type of thing this PR aims to overcome. A lot of beginners simply won't be able to do this sort of filtering in one go. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Approved in PR meeting 👍
Thanks! |
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