Godot 4.6.1
How do I reload a single scene?
I’ve setup a very basic level switcher that queue frees levels and loads in new ones and thought I could just do the same on the current level to reload it on death, but it’s not working.
I’m challenging myself to make a game start to finish in just one month and this scene management stuff has been a huge bottleneck, so any help would be appreciated.
Here’s the script
extends Node2D
var level1path = load("res://levels/level_1.tscn").instantiate()
var current_level_number = 1 #incremented in load next level
func _process(_delta: float) -> void: #debug
if Input.is_action_just_pressed("debug"):
reload_current()
func load_first_level():
add_child(level1path)
func unload_level():
var children = get_children()
for child in children:
child.queue_free()
func load_next_level():
unload_level()
var next_level_number = current_level_number + 1
var level_next = load("res://levels/level_" + str(next_level_number) + ".tscn")
add_child.call_deferred(level_next.instantiate())
current_level_number = current_level_number + 1
func reload_current():
unload_level()
add_child(level1path)
Here’s the error message when I get when reload_current runs
E 0:00:03:741 play_area.gd:29 @ reload_current(): Can’t add child ‘level 1’ to ‘PlayArea’, already has a parent ‘PlayArea’.
<C++ Error> Condition “p_child->data.parent” is true.
<C++ Source> scene/main/node.cpp:1705 @ add_child()
play_area.gd:29 @ reload_current()
play_area.gd:10 @ _process()
and here’s the error message do it a second time
E 0:00:20:355 play_area.gd:29 @ reload_current(): Required object “rp_child” is null.
<C++ Source> scene/main/node.cpp:1703 @ add_child()
play_area.gd:29 @ reload_current()
play_area.gd:10 @ _process()
I think it has something to do with freeing, but I don’t know enough to say for sure or what I could do about it if it is.