Context
- Godot version: 4.2.1.stable.mono
- Operating System: Windows 11
- No error messages or logs related to this issue
Problem
I’m having trouble with completely resetting a scene when using get_tree().reload_current_scene()
. This function seems to reload the player’s position and some other nodes, but it doesn’t delete previously instantiated objects, nor does it reinstate objects that were previously deleted.
Example
I have a WaveSpawner
script that manages the spawning of enemy waves. Each wave is an instantiated scene that follows predefined paths (road_paths
and sky_paths
). When all waves are completed, the WaveSpawner
resets the waves
array by clearing it and duplicating the default_waves
array.
However, when I reload the scene using get_tree().reload_current_scene()
, the waves
array is empty, but the previously spawned wave scenes are still present in the scene, and any deleted wave scenes are not respawned.
Here’s the relevant part of the WaveSpawner
script:
# ... (omitted code for brevity)
func wave_spawn() -> void:
if waves.is_empty():
Events.emit_signal("player_wins")
return
var wave: Wave = waves.pop_front().instantiate()
wave.road_paths = road_paths
wave.sky_paths = sky_paths
wave.wave_finished.connect(on_wave_finished)
if waves.is_empty():
wave.is_final_round = true
call_deferred("add_child", wave)
LevelManager.wave += 1
func reset_waves() -> void:
waves.clear()
for wave in default_waves:
waves.append(wave.duplicate())
### Attempts
I've tried manually calling queue_free() on all child nodes before reloading the scene, but this approach doesn't seem practical or maintainable, especially if the scene has a complex hierarchy.
I've also looked into using get_tree().change_scene_to_file() instead of reload_current_scene(), but I'm not sure if this would solve the issue or if there are any potential drawbacks.
### Questions
Why does get_tree().reload_current_scene() not reset the scene to its initial state, deleting all instantiated objects and respawning deleted ones?
Is there a more reliable way to completely reset a scene to its initial state, including all instantiated objects and deletions?
Are there any best practices or alternative approaches I should consider for managing and resetting scene states in Godot?