Area2D.get_overlapping_bodies() not detecting

I am using 4.2

So I’m working on a scene that has an array of Area2Ds to detect areas and bodies to place down new ones in unfilled spaces.

The issue I’m having is when creating a new StaticBody2D the areas don’t detect it. The areas are able to detect the same scene only when it’s placed there from the scene editor.

Just as a note, I’ve checked the collision layers and they are correct.

Interestingly enough, it completely works with Area2Ds. The issue with my code is just detecting the StaticBody2D

Here’s my code:

Script

for x in buy_array:
    Y coords
		for y in x:
			var y = get_node(y)
			if len(y.get_overlapping_areas()) == 0 and len(y.get_overlapping_bodies()) == 0:
				var BODY = _BODY.instantiate()
				BODY.ammo = 30
				BODY.modulate = "ff6b6b"
				BODY.position = position + y.position + Vector2(35, 35)
				get_tree().current_scene.add_child(BODY)
				
				break

Thank you!

I believe using get_overlapping_bodies is not instantaneous. You after to wait for the next frame to get the detected bodies.

You can wait for a frame with await get.tree().process_frame

The issue isn’t in the processing time, I’ve tested what sent (thx!) though it didn’t work sadly.

The code is able to detect bodies and areas like it’s supposed to. The issue I’m having is from instances of bodies added to the tree from scripts not being detected. Areas added to the scene tree work fine but it’s just rigid bodies.

I should’ve been more clear with that!

I’ve solved my issue. I’m just placing an area inside of my RigidBody2D that my areas can detect. While this solution isn’t perfect, it does exactly as it needs to do.

Thanks anyways for you’re help!