Area2D monitoring not working in Godot 4

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By greygoogone

I’m having difficulty getting an Area2D to detect bodies instantiated from script. I’m using the below script to instantiate static bodies, and a separate Area2D has a “body_entered” signal wired up to itself. This is the exact same code I used successfully in 3.5, but with instantiate() instead of instance().

func _unhandled_input(event):
    if event.is_action_pressed("left_click"):
        var mouse_pos = get_global_mouse_position()
        var new_body_packed_scene = load("res://godot_object.tscn")
        var new_body = new_body_packed_scene.instantiate()
        add_child(new_body)
        new_body.global_position = mouse_pos

The Area2D won’t detect bodies instantiated this way. The body entered signal doesn’t fire, nor does the get overlapping bodies method return anything. The bug has nothing to do with masks or layers because the Area2D will detect them if they are instantiated from the editor (i.e. manually placed into the scene tree).

It gets weirder: if I manually toggle ‘monitoring’ off and on during debug then the Area2D will detect all of the bodies inside of it, as if it were just seeing them for the first time. But then it goes back to not detecting new bodies until I toggle again. So for some reason the Area2D isn’t continuously monitoring even though monitoring is set to true.

Here’s a link to a zipped minimal reproduction for Godot.4.0.beta.4 Windows if you wanna take a crack at it.

:bust_in_silhouette: Reply From: greygoogone

UPDATE: This is in fact a bug and there is already an open issue on the Github. As a workaround, just force the Area2D to update every physics frame by changing any of its properties (e.g. set its collision shape’s radius equal to itself).