Can't get members out of a saved resource?

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

I have the following

extends Node

class SaveData extends Resource:
	export var data : Dictionary
	export var data2 : String

func _ready():
	n()
	l()

func n():
	var d = SaveData.new()
	d.data = {"test" : 1, "another test" : "oranges"}
	d.data2 = "String member"
	ResourceSaver.save("res://data.tres", d)

func l():
	var d = load("res://data.tres")
	print(d.data, d.data2)

When this script runs, I get an error failed to get index 'data' on base(Resource). What I don’t understand is, I’m exporting the variable in the class, I’ve used a .tres file extension just to that I can verify the data is actually being saved in the file (which it is). Why am I unable to get the data being saved here?

I do not want to use JSON to save my game data due to its limitations.

Any ideas?

:bust_in_silhouette: Reply From: alexp

Look at the warning panel at the bottom of this page. Your resource class needs to be defined in its own script.

Beware that resource files (.tres/.res) will store the path of the script they use in the file. When loaded, they will fetch and load this script as an extension of their type. This means that trying to assign a subclass, i.e. an inner class of a script (such as using the class keyword in GDScript) won’t work. Godot will not serialize the custom properties on the script subclass properly.

In [your] example, Godot would load the Node script, see that it doesn’t extend Resource, and then determine that the script failed to load for the Resource object since the types are incompatible.

:bust_in_silhouette: Reply From: toxicvgl

I found this, the warning at the bottom may help you.

We need to define the class SaveData in another script which extends Resource, I guess.