0 votes

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?

in Engine by (24 points)

2 Answers

+1 vote
Best answer

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 ^_^
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.

by (7,910 points)
selected by

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

+1 vote

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.

by (1,120 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.