0 votes

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?

in Engine by (271 points)

2 Answers

0 votes
Best answer

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.

by (185 points)
selected by
0 votes

I found this, the warning at the bottom may help you.
https://docs.godotengine.org/en/stable/tutorials/scripting/resources.html

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

by (46 points)
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.