How to prevent input on buttons below elements like an Area2D?

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

Hello,

I have a Node2D as root with the following nodes as children:

  • TextureButton A
  • TextureButton B (positioned in the center of the screen)
  • Area2D (with Sprite and CollisionShape2D as children)

If TextureButton A gets pressed, the Area2D is set to visible and gets drawn on top of the TextureButton B. But if I click on the center of the Area2D the TextureButton B below still gets triggered. I want to prevent this and just can’t figure out how to do this properly.

I have a script attached to the root node of the scene and could easily disable this button, if the TextureButton B gets pressed, but I believe there is a better method.

I would be very grateful, if anyone could give me a hint.

Thank you very much.

:bust_in_silhouette: Reply From: njamster

but I believe there is a better method.

Not really. I recommend you read up on how InputEvents work in Godot. If you want to grab and consume the InputEvent before it reaches your Buttons, you would need to do the following:

func _input(event):
	if event is InputEventMouseButton and event.pressed:
		get_tree().set_input_as_handled()

That works because the _input-method receives an event before the GUI elements. However, the above code does not check where the mouse click actually happened and if your Area2D-node was visible or not. While ensuring those things is possible, I wouldn’t say it’s “better” than simply deactivating the button here.