Skip to content
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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Input `is_anything_pressed` method #35012

wants to merge 1 commit into
base: master


Copy link

@Xrayez Xrayez commented Jan 11, 2020

Your typical "Press any key" use case using the Input singleton, can also be used to conveniently detect and switch various idle states, such as character switching to bored state if you wish so. 馃檪

func _physics_process(_delta):
	if Input.is_anything_pressed():
		$message.text = "Thanks!"

There's another way for doing so using _input:

func _input(event):
	if event.is_pressed():
		$message.text = "Thanks!"

But it might not be possible to achieve in the future according to:


Lines 191 to 194 in 02cd144

// To be removed someday, since they do not make sense for all events
virtual bool is_pressed() const;
virtual bool is_echo() const;
// ...-.

In any case, I'm here to tell that it might be a good idea to leave InputEvent.is_pressed() as is.

Test project

main/input_default.cpp Outdated
if (E->get().pressed) {
return true;

This comment has been minimized.


RebelliousX Jan 19, 2020

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.

This comment has been minimized.


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.

@Xrayez Xrayez force-pushed the Xrayez:press-any-key branch Mar 27, 2020
@Xrayez Xrayez force-pushed the Xrayez:press-any-key branch Apr 30, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

3 participants