Code works but loads of debugger errors when using get_tree().reload_current_scene()

Godot Version

4.2.1

Question

I’m following two courses where the instructor uses get_tree().reload_current_scene() to reset a level when a player (characterbody2d) enters or exits an Area2d using the body_entered or body_exited signals. The code works but I I get a massive list of errors in the debugger (red ones, not mere warnings). The instructors do not get the errors in the videos I’m watching. One of the errors mentions something about using call_deferred() instead but I have little idea what this is. Can anyone please clarify what is going on? I’m a relative newcomer to Godot.

add the errors your getting

I’ve just noticed that every time I go into/out of an Area2d, triggering the signal I get two errors:

E 0:00:04:0908 level_1.gd:15 @ _on_maze_body_exited(): This function can’t be used during the in/out signal.
<C++ Error> Condition “locked” is true.
<C++ Source> scene/2d/area_2d.cpp:328 @ _clear_monitoring()
level_1.gd:15 @ _on_maze_body_exited()

E 0:00:04:0908 level_1.gd:15 @ _on_maze_body_exited(): Removing a CollisionObject node during a physics callback is not allowed and will cause undesired behavior. Remove with call_deferred() instead.
<C++ Source> scene/2d/collision_object_2d.cpp:98 @ _notification()
level_1.gd:15 @ _on_maze_body_exited()

see if this fixes it; replace get_tree().reload_current_scene() with this one

get_tree().call_deferred("reload_current_scene")
1 Like

Thank you, I just copied and pasted your code into my game and it now works without the errors.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.