Reloading the scene from within _physics_process

Godot Version

Godot Engine v4.5.1.stable.mono.arch_linux

Question

How can you call get_tree().reload_current_scene() from within _physics_process()? I have some GDScript code where I’m checking the collisions after a move_and_slide() and at that point I need to reload the scene (poor man’s death handling code).

I get the following error if I do that

E 0:00:03:369 PlayerCharacter.detect_collisions: Cannot call method ‘reload_current_scene’ on a null value.
player_character.gd:95 @ PlayerCharacter.detect_collisions()
player_character.gd:95 @ detect_collisions()
player_character.gd:132 @ _physics_process()

Same issue if I instead emit a signal and have the signal handler do the scene reload.

I’ve also tried having the emitted signal run the following lambda in a call_deferred() : Create a 0.1 sec timer then reload the scene – no such luck. The get_tree() to create the timer works, but as soon as it’s time to reload the scene that call to get_tree() always returns null.

I have some other code that’s doing the same thing, but is running on an area2d body_entered signal and I’m not having any issues with that, so it seems like it has to do with this detection happening in the physics process since that’s the main thing that’s different. I am hoping there’s a more sustainable and elegant way in gdscript than just slapping a boolean on the script that physics_process will set on this condition, then the next time around on the regular _process() then initiate the scene reloading.

Log what get_tree() returns. It sounds like it is null, which would indicate the script’s node just isn’t in the scene tree (anymore). Perhaps you removed the node from the tree before calling get_tree()?

Post your code (with enough context) for review if this still gives you troubles.

I double checked with some more prints and it looks like because there’s actually two collisions happening the signal gets emitted twice so the second time it always fails. If I de-dupe it then the reload from physics process works.