Get_overlapping_areas not working properly

Godot 4.3 stable

Right now I’m working with 2d areas which are dragged with mouse and upon detecting each other are snapped into position. I need to make it so only 1 at a time can be “attached” to each others corresponding areas, so I’ve used the get_overlapping_areas().size() to put a limit on how many can be snapped together. I tried various ways to implement it, but no matter what it didn’t work. so I printed the array in get_overlapping_areas() and it prints 0 despite me overlapping several areas in the running project. Are the areas somehow not aware of each other despite them working as intended when it comes to snapping into each others position? Here is the code:

extends Sprite2D

var dragging = false
var of = Vector2(0, 0)
var connect = null
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
	if dragging:
		var newPos = get_global_mouse_position() - of
		position = newPos
	else:
		if connect != null:
			position = connect
			dragging = false
			connect = null


func _on_button_button_down():
	dragging = true
	of = get_global_mouse_position() - global_position

func _on_button_button_up():
	dragging = false

func is_in_same_group_as(node: Node) -> bool:
	var their_groups := node.get_groups()
	for our_group in get_groups():
		if our_group in their_groups: return true
	return false

func overlap_limit(node: Node) -> bool:
	if node.get_overlapping_areas().size() < 1: return true
	return false

func _on_area_2d_area_entered(area) -> void:
		if dragging and overlap_limit(area) and is_in_same_group_as(area):
			connect = area.get_parent().position
			connect += area.position
		print(area.get_overlapping_areas().size())

Could you show your scene tree? Maybe enable “Visible Collision Shapes” in the debug menu


here’s the scene tree(I do suspect it’s a complete mess, I’m a beginner). I tried visible collision shapes and all of them seem to be active as intended during runtime. Could the fact that area2d have been copy pasted do anything with my problem? I have checked their collision shapes as “local” if that means anything.

Are the areas on the same collision layer and mask? Are they set to monitorable and monitoring?

Not sure if this is related but thought I’d mention in case you hadn’t noticed.

The docs for get_overlapping_areas says this:

For performance reasons (collisions are all processed at the same time) this list is modified once during the physics step, not immediately after objects are moved. Consider using signals instead.

2 Likes

The solution, as you suggested, is to make sure that all areas are monitoring and monitorable, which made the “get_overlapping_areas” functional, and also add “await get_tree().process_frame” just before getting overlapping areas so the tree priorities don’t mess with it. Thank you very much for the input! :slight_smile:

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.