I am trying to write a scene manager in Godot and keep getting the following error:
Invalid get index... (on base: 'previously freed instance')
I think this error is generated because certain nodes in my scene are trying to access attributes of other nodes which were just freed in my scene change code.
Here is the scene change code which is called from a globals.gd
singleton:
func set_scene(scene_path):
# remove current scene
current_scene.queue_free()
print("FREED SCENE")
# instance new scene
var s = load(scene_path)
current_scene = s.instance()
print("INSTANCED SCENE")
# add scene instance to tree
get_tree().get_root().add_child(current_scene)
print("ADDED SCENE TO TREE")
These print statements confirm that the error is being thrown after the new scene is added to the tree. In fact, the error always has to do with references to attributes of other nodes that are accessed via variables.
My question is: Why is this error being thrown even when I free everything from the previous scene before I instance the new scene?
I may be able to protect my code from this kind of error using weakref()
, as mentioned here, but I really would prefer to avoid this since it muddies up the code. I have also tried to use call_deferred()
with set_scene()
but it doesn't help.
NOTE: I originally asked this question on the Game Dev Stack Exchange, but figured this would be a better venue. I will echo any answer I get on either site.