How to save an array of Resources with JSON

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

I have a resource called Inventory and that resource has an array of items, each of team is a resources.

export(Array, Resource) var items = [null, null, null, null]

The resource that i use for items has a lot of info: textures, strings, booleans…

I’m implementing a save/load data script that uses JSON. My problem is that when i save, my json looks like this.

{"inventory":{"items":["[Resource:1370]","[Resource:1395]","[Resource:1395]","[Resource:1395]"]}}

That means when i load i don’t get the resource that i want but instead the literal String “Resource:XXXX”.

A solution i have for this is save using an unique identifier like and id (int) or a string instead of the whole resource, then use a Match statement and load the corresponding resource.

Is there another straightforward way to do this? I feel like my solution is not flexible at all, if i want to add a lot of items i have to keep updating the paterns of the match .

:bust_in_silhouette: Reply From: kidscancode

Why do you need to convert back and forth from JSON, which doesn’t support Godot types? Godot already has a built-in method for natively saving resources - that’s how scenes, textures, animations, and every other Resource in Godot is saved - .tres.

Resources can be saved with ResourceSaver.save():

and loaded with load():