Autoloads not maintaining after scene change

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

I’m likely doing something wrong - but I don’t recall having this issue previously.

My game does a procedural generation of a universe. All the space sectors which are scenes are generated in a world creation scene. The space sector objects are put in a list of objects.

Once the world creation is done… I change scenes to the ‘main play’ scene.

But all the objects are wiped out. The autoload list contains nothing.

After reading this documentation - seems to me, what I am doing cannot possibly work as the engine seems to want to kill all the child objects that were created on a scene.

I guess autoload/singletons are only good for maintaining simple variables, and not instanced objects.

Not sure how someone should go about moving all the objects on one scene - into another scene.

mattkw80 | 2022-07-06 17:29

:bust_in_silhouette: Reply From: the_maven

In Godot Docs see:
Input and Output (I/O) » Saving games; and Scripting » Singletons (AutoLoad) - search for call_deferred.

You need to save the scene before delete it.

:bust_in_silhouette: Reply From: Inces

Resetting the scene queues free every object of the old scene. This means any list You keep will contain empty RID after reset. Instead of a list of Objects You should keep a list of dictionaries of components, that are used to create these objects. For example :

var universe = [{"source" : "res://planets/sun.tscn","size":10,"color":ColorN("red")},"position" : Vector2(10,50),.....]

and when You reset a scene You can iterate through it :

for entry in universe:
      var inst = load(entry["source"]).instance()
      inst.size = entry["size"]
      inst.global_position = entry["position"]

and so on

Generally resetting the scene is primitive and problematic option of changing a level. It might be better to keep high scope node above your level, that will manage changing of scenes without resetting it. This way You can release low scope level and keep permament objects in high scope menager.

Inces | 2022-07-08 15:13