I've been having a hard time trying to figure out how to save and load scene's state. I'm trying to move back and forth between scenes without having them reset to their initial state each time. Ideally, id like to load a scene as it was when the transition was triggered.
I've been working off of this simple function in my global script:
var currentScene
func setScene(scene):
currentScene.queue_free()
var newScene = ResourceLoader.load(scene)
currentScene = newScene.instance()
get_tree().get_root().add_child(currentScene)
This just switches the scene, but I've been trying stuff like:
var sceneState
func setScene(scene):
sceneState = get_tree().get_current_scene()
get_tree().get_root().remove_child(sceneState)
var newScene = load(scene).instance()
get_tree().get_root().add_child(newScene )
get_tree().set_current_scene( newScene )
func load_scene():
if sceneState != null:
get_tree().get_current_scene().queue_free()
get_tree().get_root().add_child(sceneState)
sceneState = null
This one seems right, but it crashes with a null reference exception, pointing to scene scripts where I call get_node()
Any tips or help would be much appreciated. Thanks in advance