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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Input.is_action_just_pressed is true on two frames with an InputEventMouseButton #43492

Closed
Rubonnek opened this issue Nov 13, 2020 · 4 comments
Closed

Comments

@Rubonnek
Copy link
Member

Rubonnek commented Nov 13, 2020

Godot version:

3.2 branch at commit adf2dab

OS/device including version:

Arch Linux

Issue description:

Input.is_action_just_pressed returns true on two frames for the same action with an InputEventMouseButton which is unexpected as it should only be true on one frame.

Steps to reproduce:

  1. Run the attached project
  2. Start moving your mouse inside the project window (don't stop moving it).
  3. Keep moving the mouse and right clicking the project window and holding the right click for more than two frames.
  4. On some occasions Input.is_action_just_pressed will be true for two frames.

Minimal reproduction project:

rbb.zip

@Calinou
Copy link
Member

Calinou commented Nov 13, 2020

Does this occur if you call Input.set_use_accumulated_input(false) in any script's _ready() function? By default, input accumulation is enabled.

@Rubonnek
Copy link
Member Author

Rubonnek commented Nov 14, 2020

Yes. In fact, running Input.set_use_accumulated_input(false) as the last line in the _ready function of the the attached Control.gd makes the issue easier to reproduce. I'm getting up 3 frames where Input.is_action_just_pressed is true.

@rosshadden
Copy link
Contributor

I encountered this as well and worked around it using the event.is_action_type in my _input functions to return early if it is not an action event. This wouldn't work in situations where one _input handler handles both action events and mouse events, so duck checking the event type is probably a more generally applicable workaround.

@Rubonnek Rubonnek removed their assignment Jul 26, 2021
@Rubonnek
Copy link
Member Author

Rubonnek commented Mar 5, 2022

@rosshadden is right. In this particular case, the root cause of this issue is simply that I was using the Input singleton which has access to all the events that are happening in the current frame. I'm not inspecting the InputEvent that is actually handed to the _gui_input function and by moving the mouse and right clicking at the same time two InputEvent objects will be passed to the _gui_input function at the same frame, which in turn will make Input.is_action_just_pressed("right_click_bug") return true twice because we are still in the same frame where the right click was pressed and when the mouse was moved.

This can be proved by using the event just as @rosshadden suggested.

Printing the InputEvent as follows confirms this is the case as well:

func _gui_input(_p_input_event : InputEvent) -> void:
	if Input.is_action_just_pressed("right_click_bug"):
		count += 1
		message = "Number of right clicks detected: " + str(count)
		print(message)
		$Label.set_text("Number of right clicks detected: " + str(count))
		print(_p_input_event)

The following output can be gathered under the same frame:

Number of right clicks detected: 1
[InputEventMouseButton:1807]
Number of right clicks detected: 2
[InputEventMouseMotion:1810]

By the time I opened this ticket I wasn't aware that any of the input related functions (i.e. _input, _unhandled_input, __unhandled_key_input, and _gui_input) could fire more than once under the same frame. And in general, using the Input singleton within any of these function could be considered bad practice (if used, it should be used carefully on special cases where more than one input needs to be inspected, just like @rosshadden pointed out). I'll be opening a new ticket to document this. I think this should be or could be included in our best practices guide.

I'm closing this ticket since there is no bug here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants