0 votes

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.

Godot version 4.0.beta.4
in Engine by (19 points)
edited by

1 Answer

0 votes
Best answer

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).

by (19 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.