is it possible to save resources that are within another resource?

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

is it possible to save resources that are within another resource? For example I have a save file resource that contains another resource (player inventory) and when I load the save the inventory resource doesn’t get saved along with the overall save file. My save file script:

extends Resource
class_name SaveFile

export(int) var map_seed = 0
export(int) var depth = 1
export(Vector2) var grid_pos = Vector2()
export(float) var ang = 0
export(Vector3) var rotation = Vector3()
export(Resource) var attributes = null
export(Resource) var inventory = null
export(Resource) var equipment = null

I create a new inventory ( during new save file creation and when I load up the game it doesn’t seem to save the inventory resource (or any of the other resource exports). Am I doing something wrong or can subresources just not be saved?

:bust_in_silhouette: Reply From: Parv

ive had the same question myself.
turns out you just need to use the flag FLAG_BUNDLE_RESOURCES as specified on the docs, because by default the tres/res file will just save a reference to the path of subresources.
so it will look like this:, resource, ResourceSaver.FLAG_BUNDLE_RESOURCES)

also for some reason the opposite load function will not work if you try to cast it like:
var save = ResourceLoader.load(path) as SaveResource which will return null.
so just use duck typing when loading it back.

Thank you so much, it works!

GameBubby | 2021-12-17 04:06

What do you mean with ducktyping? I can not get any data back from user ResourceLoader.load(path).

voylin | 2022-02-05 06:48

dont try to cast it to your save class when loading since for some reason it will break it.

Parv | 2022-02-05 18:28

A bit late, but:

The reason you can’t cast is that bundling resources re-declares that resource locally. Any resource kept in that file is now of type res_file_name_goes_here/res_name_goes_here.tres, even the very resource of the file itself, and Godot treats the bundled resources and the script resources as completely different objects. So, because you’re trying to cast this bundled resource as a script resource, Godot thinks you’re saying that this apple is an orange, and flips out.

SeaFishelle | 2023-02-08 05:31

Trying to figure this out in Godot 4, but can’t seem to understand it. Would you mind explaining this in more detail?

Currently, I’m saving a resource (player data) with a sub resource (inventory data).
Looking at the saved file in a text editor confirms that I am saving these sub resources,
or at least references of them? (I’m using the flag: FLAG_BUNDLE_RESOURCES).

(I’ll try to provide more detail once I’m next to my computer)

But anyway, upon loading this resource, I get an error saying that the sub class is hiding a script (if I remember correctly, but again, I’ll double check once I’m by my PC).

So I guess the problem is, how do I load a resource which has multiple sub resources nested inside it?

Thanks, and sry for any confusion.

Queble | 2023-03-28 19:56

1 Like