Make CollisionShape2D receive mouse event first

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By reonZ

I am trying to make my sprites clickable, i added an Area2D and a CollisionShape2D to it and then connected the CollisionObject2D.input_event.

When i click on it it works fine (filtered other event types out), but if i have a _unhandled_input anywhere on the parent scene, it is still called and even before the Area2D event weirdly enough.

How can i make it so my mouse event is handled by my sprite Area2D first and then consumed ?

:bust_in_silhouette: Reply From: Zylann

As the doc describes, _unhandled_input is indeed called before collision shapes Using InputEvent — Godot Engine (stable) documentation in English

However, note that _input and _unhandled_input aren’t bound by any particular collision shape, rectangle or area, they are globally called. If the thing listening to _unhandled_input is not “globally listening” (if it’s actually a GUI button for example), you may use another event.

Otherwise I don’t have an easy idea… I would have thought _unhandled_input to be called after everything.
Maybe you could add a Control node to your sprite instead of an Area2D so that it gets the event first?
Or maybe instead of using _unhandled_input, queue the events in an array, and dequeue them yourself later so you can decide which is processed first… but that would be messy.

Alright thanks for the answer, i just started using godot so i know pretty much nothing but i am kind of thrown by the fact that an unhandled event handler would be called before any potential other event, does not make much sense to me.

reonZ | 2020-03-08 18:03

You could also work this around by not using events, but instead calling the Physics2DServer to check if there is a shape on top of your mouse.

Zylann | 2020-03-08 18:06