Stop propagation of click event on sprite

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

I have ships flying in a background. I want to be able to select the ship by clicking on it and then click on a map location to select the ship destination. However, when I click on the ship to select it, the even is also later captured by the root node (or the parent area2d if I’m using one).
I want that the background (root node or parent area2d) only gets the event if I didn’t click on a ship. How do I do that?

Would you mind sharing a bit more on this?
It would help to tell us what kind of node the ship is and what signals you are reacting to. Maybe the signal also emits a variable with the signal that you can check on, like CollisionObject2Ds input_event?
That way you could raise a flag to ignore this event from being reacted on further but I don’t think you can stop signal propagation in godots hierarchie like you do in html and javascript.

ipdramon | 2021-10-30 10:13

The ship is a kinneticbody2d, it contains a sprite and a collider2d. I was connecting to the input_event() of the kinnecticbody2d. I found a better solution and I posted the answer. below.
thank you.

erebrus | 2021-10-30 11:12

:bust_in_silhouette: Reply From: erebrus

I ended up finding the following solution.

Instead of using the input event from the collider, I use the generic _input(event) function and check if the click is inside the rect. Also, I mark the event as handled.

func _input(event):
	if event is InputEventMouseButton and event.button_index == BUTTON_LEFT:
		if $Sprite.get_rect().has_point(to_local(get_global_mouse_position())):
			if event.is_pressed():
				Global.emit_signal("ship_selected", self)
			get_tree().get_root().set_input_as_handled()
	

Then in the GUI I capture the unhandled input and act accordingly:

func _unhandled_input(event):
	if event is InputEventMouseButton and event.button_index == BUTTON_LEFT and not event.is_pressed():
		if selected_ship != null:
			selected_ship.go_to(to_local(get_global_mouse_position()))