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

Two overlapping Area2D / pickable CollisionObjects both receive input event #29825

Closed
p10tr3k opened this issue Jun 16, 2019 · 11 comments · Fixed by #75688
Closed

Two overlapping Area2D / pickable CollisionObjects both receive input event #29825

p10tr3k opened this issue Jun 16, 2019 · 11 comments · Fixed by #75688

Comments

@p10tr3k
Copy link
Contributor

p10tr3k commented Jun 16, 2019

Godot version:
3.1

OS/device including version:
Windows 10

Issue description:
I have two overlapping Area2d with rectangle shape. When I click them, both got input event. The one at the bottom gets the event first.
So there is no way to just click the top one. Moreover the get_tree().set_input_as_handled() doesn't stop propagating this event.

Minimal reproduction project:
godotArea2dOverlapping.zip

@mrcdk
Copy link
Contributor

mrcdk commented Jun 16, 2019

That's expected. The signals have already been emitted so the event can't be set as handled. If you need more control over it you'll need to use _input_event()

@p10tr3k
Copy link
Contributor Author

p10tr3k commented Jun 16, 2019

It seems to me that Area2D should have some kind mechanism to be able to click just the top most

@p10tr3k
Copy link
Contributor Author

p10tr3k commented Jun 16, 2019

For some time I am searching the web trying to find generic solution for this, but I only find people with similar question.

@p10tr3k
Copy link
Contributor Author

p10tr3k commented Jun 16, 2019

I wrote this code and it seems to work in my case, but I do not know if it will work for all cases. Still i think Area2D should do this out of the box.

func _on_Area2D_input_event(viewport, event, shape_idx):
	if event is InputEventMouseButton and event.is_pressed():
		var areas = get_overlapping_areas()
		var is_on_top = true
		if areas.size() > 0:
			for area in areas:
				if not is_greater_than(area) and area.is_visible_in_tree():
					is_on_top = false				
		if is_on_top:
			emit_signal("mouse_event", event)

@Winsalot
Copy link

Winsalot commented Sep 17, 2020

I have also ran into this issue, so I though I would share my own workaround:

var mouse_entered = false

func _unhandled_input(event):
	if event.is_action_pressed("mouse_left_click") && \
	mouse_entered:
		# do here whatever should happen when you click on that node
		self.get_tree().set_input_as_handled()

func _on_Area2D_mouse_entered():
	mouse_entered = true

func _on_Area2D_mouse_exited():
	mouse_entered = false

Here I only use Area2D signals to check if the mouse is inside the CollisionShape2D. The input event is handled directly by the node without emitting any signals.

@lukepass
Copy link

I'm having the same issue and I think that Area2D should have a machanism to mask the input of the other Area2D items below.

@Winsalot thanks for the workaround! However how can I know the order in which the areas are clicked? As far as I can tell if I click on an area the order of input is random, correct?

@Sauermann
Copy link
Contributor

looks like a duplicate of #16042 and has more detail info

@akien-mga akien-mga changed the title Two overlapping Area2d recive input event Two overlapping Area2D / pickable CollisionObjects both receive input event Jun 7, 2023
@crysan

This comment was marked as off-topic.

@cjmaxik

This comment was marked as off-topic.

@Calinou
Copy link
Member

Calinou commented Nov 27, 2023

Please don't bump issues without contributing significant new information. Use the 👍 reaction button on the first post instead.

@mnemoli
Copy link
Contributor

mnemoli commented Dec 3, 2023

@Calinou can I give a gentle nudge on PR review for this?

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

Successfully merging a pull request may close this issue.