Saving array of objects as resource

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

Hello,
i’m trying to create a saving system using custom resources: each node has a “stats” resource, that will be saved with ResourceSaver, a relatively simple task for variable like float or strings.
However, some of the stats of these nodes are actually link to other nodes (e.g: each Nation has a city_list variable, which is an array of objects that point to the city nodes belonging to that nation), and i do not know how to put them in the custom made resource i created.

In fact, extending Resource, inside the nation stats city_list cannot be an array of nodes (not possible inside a resource), neither an array of objects (possible, but it cannot be saved).
I was thinking about changing city_list into a list of paths that point at the saved resource file of the city, but it seems like things would spiral up in complexity in this way.

Am i missing something that would make this task easier?

:bust_in_silhouette: Reply From: barbaros

you could try using json

Care to elaborate?
I’ve read Json are more complex to use as save files actually, cause they don’t support many variable types

Andrea | 2021-08-27 21:26

:bust_in_silhouette: Reply From: Andrea

It seems there is no way to store a node inside a resource, so my workaround is to store the tree path of the node instead.
Whenever i got a resource of the type var node=get_node(path), if i need to save it in a resource i actually store it as path

It works, but during save game loading it requires 2 step:

  1. instance all the nodes that you saved + load the resources and “put” them in the instanced node
  2. when finished instancing ALL nodes, go through them again and get the linked nodes through path (var node=get_node(saverResource.path)

This is because, if you dont wait for all nodes to be instanced, some of the nodes might not be instanced yet