Why do Mouse InputEvents Sometimes Slip Through?

Godot Version

4.2.1

Question

I have an Area2D that is supposed to catch LMB clicks and process them. If a LMB click doesn’t land on this Area2D, another Area2D behind it is supposed to make the catch. With this in mind, I wrote the below code.

This code allows LMB events to slip through (to be spuriously caught by the element behind), about 5% of the time. But, if I move the viewport.set_input_as_handled() line so that it comes immediately after if (event is InputEventMouseButton), it works perfectly. Does anyone know why that might be? Seems like weird behavior. Thanks for any insight!

(also I know the indentation below looks a bit wrong - in my editor it is correct. Just a little trouble with formatting here)


func _on_area_2d_screwhead_input_event(viewport, event, _shape_idx):
         viewport.set_input_as_handled()
	         if (event is InputEventMouseButton):

		
		if !is_active:
			return
		if (event.is_pressed() and event.button_index == MOUSE_BUTTON_LEFT):
			set_is_screwing(true)
		if (event.is_released() and event.button_index == MOUSE_BUTTON_LEFT):
			if !event.is_echo:
				set_is_screwing(false)
...

Well if if !is_active: is true then it returns early and if set_input_as_handled is at the bottom then it won’t be called in those cases (?)

Maybe I’m describing things badly. The code that works has viewport.set_input_as_handled() immediately after if (event is InputEventMouseButton):and immediately before if !is_active:

Oh my bad, I misunderstood it.

The order in which ColliosionObject2D-nodes receive input events in their *_input_event function is random.
You can make the order deterministic by using Viewport.physics_object_picking_sort.

1 Like

Ah, that’s so good to know, thank you! If I could ask a followup, do you have any idea why it would only break through so rarely? Some kind of pseudo to the randomness?

Can’t answer that … Haven’t looked the this part of the Physics engine.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.