How to save changes when switching scenes?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By redstonecreeper8

The game I’m making has scene transitions when you enter different areas. However, upon returning to a scene you’ve already been in, it reverts back to the way it was before runtime. If I just needed to store a few variables, I could keep them loaded in a singleton, but I plan to expand on the game quite a bit and it would be really nice to have a way to just save the entire scene. Is this feasible for large maps? If so, how could I do it? Thanks in advance :slight_smile:

:bust_in_silhouette: Reply From: Adam_S

You can save a scene like this:

func save_scene():
    var file_path = "res://your_scene.tscn"
    var scene = PackedScene.new()
    scene.pack(root_node_of_your_scene)
    ResourceSaver.save(file_path,scene)

Note that when instancing or removing nodes from this scene, you have to set the owner or it won’t get saved.

When instancing a new node:

$new_node.owner = root_node_of_your_scene

When removing a node:

$node_to_remove.owner = null

And make sure you don’t override your original scene.

How do you then load the packed scene?

attacker | 2022-07-08 18:40