Select front area based on y sorting

Godot Version

4.3

Question

Hi, I’m working on a top-down RTS and I am trying to program a selection system so far
the problem is the input event and area entered methods are not working
image
image
if I click on the middle of 2 areas the result is the same, so how do I handle this situation?
my expectation for image 1: blue
my expectation for image 2: green

So when you are clicking on an overlapping area, both of them are selected, but you only want to select the one in front?

Learn about the mouse_filter property of your node and see if setting it to stop will help resolve your problem. If you put the mouse_filter to stop, the mouse click signal will not also trigger in any background nodes.

mouse_filter is for Control and not Area2D
I wrote something and I think this is the best way, just by checking the y value and selecting the Area2d with the highest value:

extends Area2D

var select: Area2D
var items: Array[Area2D]

func _physics_process(delta: float) -> void:
	global_position = get_global_mouse_position()

func _input(event: InputEvent) -> void:
	if event is InputEventScreenTouch and event.is_pressed():
		set_deferred("monitoring", true)

func _on_area_entered(area: Area2D) -> void:
	items.append(area)
	set_deferred("monitoring", false)
	for i in items:
		if not select:
			select = i
			continue
		if select.position.y > i.position.y: continue
		if select.position.y <= i.position.y:
			select = i
	set_deferred("items", null)
	print(select)

func _on_area_exited(area: Area2D) -> void:
	pass # Replace with function body.
1 Like