I have this specific Resource script:
class_name MyDataStructure
extends Resource
@export var title: String = "Default Title"
@export var medals_per_world = {
"world1": {
"bronze": 0,
"silver": 0,
"gold": 0,
},
# Etc.
}
func _init(_some_title: String):
title = _some_title
And I'm trying to save/load it with these methods:
const SAVE_DATA_PATH := "user://save_game.tres"
var _data: MyDataStructure
func _on_button_save_pressed():
ResourceSaver.save(_data, SAVE_DATA_PATH)
func _on_button_load_pressed():
if not ResourceLoader.exists(SAVE_DATA_PATH):
return
_data = load(SAVE_DATA_PATH)
Saving works fine and gives me this type of file:
[gd_resource type="Resource" script_class="MyDataStructure" load_steps=2 format=3]
[ext_resource type="Script" path="res://my_data_structure.gd" id="1_e2e3s"]
[resource]
script = ExtResource("1_e2e3s")
title = "Foo title"
medals_per_world = {
"world1": {
"bronze": 3,
"gold": 2,
"silver": 4
}
}
Loading it fails with this error:
E 0:00:07:0699 root.gd:31 @ _on_button_load_pressed(): Error constructing a GDScriptInstance. <C++ Error> Method/function failed. Returning: nullptr <C++ Source> modules/gdscript/gdscript.cpp:183 @ _create_instance() <Stack Trace> root.gd:31 @ _on_button_load_pressed()
I recently introduced the nested dictionary medals_per_world so perhaps that's causing me issues?
Either way, how can I properly load such a file with the ResourceLoader? The fact that it saves properly suggests that loading it should also be possible? Or is my only workaround/solution to define the entire data structure with dto Resource scripts for each nested level if the dictionary is the cause?
I'm working with Godot 4 so can use any recently released GDScript feature if needed.