+1 vote

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 (Inventory.new()) 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?

in Engine by (16 points)

1 Answer

+1 vote
Best answer

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:
ResourceSaver.save(path, 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.

by (46 points)
selected by

Thank you so much, it works!

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

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

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.