Saving nested resources without errors

Godot Version

4.5

Question

Hello! I’ve been trying to implement a saving system in my game using custom resources, but whichever way I do it either doesn’t work or throws errors.

So I have a custom persistent data resource, which holds an array of defeated enemies and a Player resource. The player resource holds all player stats, equipped items, and inventory. Every item (equipped and in inventory) has a NarrationLine resource attached to it.

So I’m using a global DataManager script to save and load the game, which looks like this:

var persistent_data : PersistentDataResource

var save_path := "user://data.tres"

func _ready():
	persistent_data = load("res://Resources/persistent_data.tres")
	
	load_game()

func save_game():
	ResourceSaver.save(persistent_data, save_path, ResourceSaver.FLAG_BUNDLE_RESOURCES)
	
func load_game():
	if FileAccess.file_exists(save_path):
		persistent_data = ResourceLoader.load(save_path)
	

When I tried using this script without the ResourceSaver.FLAG_BUNDLE_RESOURCES, the defeated enemy data within persistent data saved fine (as it is just an array of Strings), but the Player data did not save at all. But now that I added ResourceSaver.FLAG_BUNDLE_RESOURCES, on starting the game it throws an error “Class “NarrationLine” hides a global script class.

I’m aware there are similar issues brought up before, but I was wondering if Godot 4.5 fixed it or if there is a way to save nested resources without workarounds.

Would be grateful for any help!

Check for circular dependencies.

2 Likes

Hello! Thank you for your reply!

I honestly don’t think there are any circular dependencies in here? To be more clear, the Persistent Data resource that I am saving hold two arrays of Strings and the Player Resource. The Player Resource hold variables (float, int, String), a Weapon Resource and an Armor Resource (which inherit from the Item Resource), and an Inventory Resource. The Inventory Resource holds an array of Item Resources. Every Item Resource holds several NarrationFull resources, which holds an array of NarrationLines. Narration Line holds a String and several variables.

I don’t see any circular dependacies in here, but perhaps I am mistaken?

Any more fixes for this? I’m at the end of my rope here :frowning:

Can you make a minimal reproduction project?

Hello again! So I made a project showcasing my issue, which can be found here: GitHub - biphasicSleeper/save-test: saving test for godot 4.5 (hopefully I uploaded it right, I’ve never done it before).

When you try to save without using the bundle resources flag, the inventory does not save. But when you use the flag, it throws a “Class “Item” hides a global script class.” error.