Search code examples
collision-detectionareagodotoverlapping

Godot: area_entered() signal not fired for overlapping areas


I have a highlighter Area2D that snaps to the tilemap grid (think of a flashlight pointing to a specific tile), and I would like to have it detect which area is currently highlighted using the area_entered() and area_exited() signals:

# Highlighter.gd

extends Area2D

var highlighted_area

_on_Highlighter_area_entered(area):
    highlighted_area = area

_on_Highlighter_area_exited(area):
    highlighted_area = null


This is working as intended, except when I move from one area to an overlapping area. In that case the exiting signal fires but the entering signal for the adjacent (overlapping) area DOES NOT fire, hence highlighted_area will be stuck with null. See figure below:

enter image description here

Why is the enter signal not fired for overlapping areas? Any ideas how to get the signals to work properly?


Solution

  • Double check the monitoring and monitorable properties of the Area2D.

    Also, be aware that you are only storing one area in the script. Thus, if it has one area registered, and another area enters, it will override the first one. Furthermore, if one area exists, you will have null, even if there are still areas overlapping.

    You could, of course, keep an array of currently overlapping areas.


    However, I want to suggest something different. If you want to find out what is on a given point, you can use intersect_point. Example:

    var space = get_world_2d().direct_space_state
    var mouse_pos = get_global_mouse_position()
    var results = space.intersect_point(mouse_pos, 32, [], 2147483647, false, true)
    for result in results:
        print(result.collider)
    

    Here we are asking for at most 32 collisions, with no exclusion ([]), on every layer (2147483647), excluding bodies false, and including areas true.