Load Instances from Save file

Godot Version

V4.3

Question

So I can see that you can save/load strings or ints or floats, such as within a dictionary, but haven’t been able to do the same with objects of a class / instances of a class (whichever you prefer). ie

So when I store it into the save_data, which will then be saved to the file, it registers as being in the save_data, with like a print function, but when I go to load it, its a null value.

The reason I need this is because I use Procedural Generation to create random entities based on a single resource file, with different colors, texture regions, and names, and then want to save that data to a file, but thus far, I have to convert all that data into strings or floats, and load that back, which is a real hassle.

So I want to load back these instances after I create them at runtime, back into the game.

How might I go about doing this? (if at all, that is)

everything that is a Resource (or inherits it) is easily serializable.
so either you store all your procedural parameters in a custom resource and serialize that
or you can look into packing scenes and saving them as tscn directly (make sure all nodes created at runtime have their owner set to the root node of the scene you want to pack)

The issue is the instances are not part of a scene, but are created separate from the scenes and then loaded into them. So there is less coupling of assets so that if I make changes to the entities, I don’t have to spend hours fixing bugs cropping up in other places.

They inherit basic things like the sprite sheet from the resource, but otherwise all other data for them is generated by functions outside of the resource file, and then saved to the instances for reloading later. But I want to save the entire instance to the save file, rather than save all the variables it holds and reload each individual value to each individual instance.

So I’m looking for a way to save the entire object to the save file. In my old python game, that was totally possible to do, I just haven’t figured out how to do it here in Godot… inspired by python.

seems like you need scene packing.
Creating nodes or resources separately does not prevent you from using scene packing. Scenes are only “resources” on the filesystem. Once they’re in the SceneTree, they’re just regular nodes :slight_smile:
So you could say that nothing is a scene actually, and anything could still become one

The issue is my created assets at runtime are not attached to a node, resource, or scene. They inherit basic variables from a resource and from other Godot scripts, but once created the operate independently. So unless I can generate a node, resource, or scene at runtime, and pack the created instances into them, I wouldn’t be able to do your suggested method.

You can absolutely generate nodes and resources at runtime (as I said, scenes don’t mean anything in the runtime)
If your created assets are not attached to anything, how are they being used ?
Could you provide an example ? It will be easier to point you in the right direction

When they are created, they are added to an array, and then it goes through that array each “turn” allowing them to act. When they are dead, they are removed from that array. The array is attached to a script, and acts as a hub/bus wherein the rest of the codebase flows through.

So all my entities are created at runtime, stored in an array, and then are used/move around from there.

I’m not sure if that explains anything.

ie when created:
Entities_List.append(New_Entity)

for Entity in Entities_List:
if Entity.ai_component:
perform_ai_decision()

what kind of objects are Entities ? Resources ? Nodes ?

Entities inherit some basic data from a resource file. One per entity type. Mostly so that I can easily load in an alpha texture map, then manipulate the data further from there.

Once the entity is created from the resource file, they get member classes applied (something akin to entity component systems or ECS for short) and further data gained.

After all that, they no longer are attached to any specific resource, node, scene, but can appear in nodes or scenes in a transient/temporary manner.