area_enter signal is triggered, but overlaps_area is False?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Suchaaver Chahal
:warning: Old Version Published before Godot 3 was released.

So, recently I got my desired Area2D objects setup in a scene. The problem is, when I put a node that overlaps another node, both their area_enter signals are triggered, but if I immediately check that `node1.overlaps_area(node2)’ it always returns false.

Code:

        gun_base = get_node("gun_base")
		var new_attachment = attachment.instance()
		new_attachment.get_node("Sprite").set_texture(current_mode_tex)
		var shape = RectangleShape2D.new()
		shape.set_extents(current_mode_tex.get_size()/2)
		new_attachment.add_shape(shape)
		new_attachment.translate(event.pos)
	
		add_child(new_attachment)
        print(gun_base.get_overlapping_areas())
		if(!new_attachment.overlaps_area(gun_base)):
			new_attachment.free()

However, I print out the overlapping areas, and on the next placement of an overlapping node, it returns an array containing the previous overlapping nodes. So if I just place the 4th node that should overlap Node 1, it has an array of node 1, node 2, and node 3. No node 4, even though the area_enter signal fires. What’s going on?

Reddit - Dive into anything

atze | 2016-11-14 13:05

:bust_in_silhouette: Reply From: Warlaan

Sounds like it’s an “area enter” signal, not an “area entered” signal. In other words the signal is triggered before the node is done entering the area, so it is about to enter but isn’t inside yet.
That makes sense if you want to write code that prevents objects from entering an area, because if you push them away in the area_enter-callback they will never be officially inside the area. That would be impossible if the area_enter-signal was fired after adding the node to the overlapping areas.

:bust_in_silhouette: Reply From: eons

Both signals will trigger if both areas are monitored, monitoring, in the same layers and looking at the same layers (mask).

If I look it like Tumiki Fighters ship pieces, I would make it in different layers and masks, and changing them on area_enter, and just base and attached monitoring for more (and with groups for filtering too).


You won’t get immediate overlap information because there are many threaded steps between adding the nodes and getting them processed on the scene.

I have never used it (yet) but for that particular situation you can use Shape2D methods to get a simple quick overlap check between shapes:

shape.collide(new_attachment.get_transform(),gun_base.get_shape(0),gun_base.get_transform())    

ps: If this is the only time you will be checking overlaps, you may find a way to use shapes without Areas and getting a solution for your previous question :slight_smile:
Look the “shower of bullets demo”, uses an area for enter signals but if you dont need constant monitoring and other things provided by Area2D, may be what you are looking for

EDIT:
PPS:
If you need shape’s visual debug information, add a CollisionShape2D child to the area and add the same shape to it with set_shape, is the only way to get that, for now.

The code snippet you gave for shape collide actually did the trick. Thanks for the extra explanation!

Suchaaver Chahal | 2016-11-15 16:37