Get_overlapping_areas() returns empty array

Godot Version



Hi There,
I want to know if there is an Area2D in a certain position of the viewport…
I see the way to do it is with get_overlapping_areas()… so I do next:

The main tree is a Node2D with 2 childs positioned at the same coordinates, every child is a scene with tree (Area2D),and 2 childs Sprite2D and CollisionShape2D.

Just trying to understand if get_overlapping_areas() is the function I need…

if I code: func _ready():
var area1:Area2D=get_node(“1”) as Area2D
var area2:Area2D=get_node(“2”) as Area2D

var areas= area1.get_overlapping_areas()


What I am not understanding?, it there a easier way to know if any object is in a certain position?

Thanks in advance

Did you run it in _physics_process function?
And make sure Node1’s collision_mask contains Node2’s collision_layer.

1 Like

Oh Thanks, now I see that the problem is I am trying to do it in the Ready function… I see it also works in _process and _physics_process… which I still don’t understand is why in the Ready function is not working, my idea was to make a random dungeon tile map just inside the Ready funcion…

Check document Area2D.get_overlapping_areas, it explains the reason.

If I want to place an Sprite2D on the right hand side of an existing one, how can I check if the space is empty or busy with another Sprite2D?, I can only do that by checking the overlapping area2D in the process or physic_process funcs then?
is there another way to check that in the ready function?

You can compare positions with other nodes.
Use Vector2.distance_to or Vector2.distance_squared_to to get distance.