#Does scene.pack() save the internal variables of node scripts?

2 messages · Page 1 of 1 (latest)

sly elk
#

In my game, I have the following process:

  1. Generate overworld map containing battle encounters
  2. Load battle scene when getting an encounter
  3. When the battle is over, go back to the overworld map

The way I am doing this right now is by saving the overworld scene as a PackedScene when entering a battle, and loading this packed scene when leaving the battle later. I use the following save / load functions:

func save_scene(_scene_name):
var packed_scene = PackedScene.new()
packed_scene.pack(get_tree().get_current_scene())
ResourceSaver.save(_scene_name, packed_scene)

func load_scene(_scene_name,_current_scene):
# Load the PackedScene resource
var packed_scene = load(_scene_name)
# Instance the scene
var scene_instance = packed_scene.instance()
_current_scene.queue_free()
get_tree().get_root().add_child(scene_instance)

I also read somewhere that it's important to have the owner of the nodes by the scene that I save, so I make sure to do it as well.

The issue is that when I load the scene later, all scripts part of the scene have reset themselves. Variables are all back to their default values (although nodes generated previously are still there, so the right scene got saved and loaded, with the right nodes). This completely breaks the scene.

I really don't understand what I'm doing wrong... Is scene.pack() just not the right tool, or am I missing something?gdthonking

sinful yarrow
#

I would suggest just removing the scene without freeing it. Then you can just add it back in later. No need to pack the scene