0 votes

Hello everybody,
I have a small issue with the saving and loading of ressources… Here is exemple :

Class RessourceA :

tool
extends Resource

class_name ResourceA

var data1 : int
var data2 : int
var data3 : int

Class RessourceB :

tool
extends Resource

class_name ResourceB

var resource_a_preload = load("res://ResourceA.gd")
var resource_a_array : Array

func run():
    for i in range(10):
        var res = resource_a_preload.new()
        res.data1 = i + i
        res.data2 = i * i
        res.data3 = res.data1 + res.data2
        print("Data1 %d | Data2 %d | Data3 %d" % [res.data1, res.data2, res.data3])

Class Handler :

tool
extends Control
var resource_b_preload = preload("res://ResourceB.gd")
func _on_SaveFSMButton_pressed():
    resource_b_preload.new().run()
    var result = ResourceSaver.save("res://file.tres", resource_b_preload)
    if result != 0:
        print("Something goes wrong !")

File’s content :

[gd_resource type="GDScript" format=2]

[resource]
script/source = "tool
extends Resource

class_name ResourceB

var resource_a_preload = load(\"res://ResourceA.gd\")
var resource_a_array : Array

func run():
    for i in range(10):
        var res = resource_a_preload.new()
        res.data1 = i + i
        res.data2 = i * i
        res.data3 = res.data1 + res.data2
        print(\"Data1 %d | Data2 %d | Data3 %d\" % [res.data1, res.data2, res.data3])
"

As you can see, my file has the source code but not the data stored in ResourceB.
What am I doing wrong when I'm saving the file ?
Thank you in advance

Godot version 3.2
in Engine by (81 points)
edited by

not super expert on the topic, but resourcebpreload seems to be the proloaded resource only.
does it work if you change it with

var res=resource_b_preload.new()
res.run()
var result = Resource.save("res://file.tres", res)

?

You're totally right... facepalm
I fixed it but know my file looks like that :

[gd_resource type="Resource" load_steps=2 format=2]

[ext_resource path="res://addons/TyFSM/Content/ResourceB.gd" type="Script" id=1]

[resource]
script = ExtResource( 1 )

I tried to load my resource like this:

var resource = ResourceLoader.load("res://file.tres")
print(resource.resource_a_array.size())

and like this :

var resource = resource_b_preload.new()
resource = ResourceLoader.load("res://file.tres")
print(resource.resource_a_array.size())

The result of the print, in both case, is : 0

mmmm, i can see that you decleared the resource_a_array but never filled, so 0 should be correct.
did you forget about it or simply positionated in another part of the code?

What's wrong with me...
I add at the end of my run() method :

resource_a_array.append(res)

But the file is still the same

i think another error could be that you used Resource.save(path, res) instead of ResourceSaver.save(path, res)
https://docs.godotengine.org/en/stable/classes/class_resourcesaver.html
I'm not 100% sure though, as i cant find any documentation on Resource.save() method

Sorry, it was a typo... I'm already using ResourceSaver.save.
I fixed my first message

does it work if you store less complex data?
like try adding 2 test variable in the resource_b

var test1=1
var test2=0
func run():
  test2=2

does the loader correctly shows the value 1 for test1 and value 2 for test2?

I tried but nothing changed...
I think that the issue is from the saving beceause the file is "empty".
There is some information about the resource but no data

on below tutorial, the guy uses
var res : Resource =load(path), instead of using the resourceloader.
Maybe try that?
https://www.youtube.com/watch?v=ML-hiNytIqE

if that doesnt work either, i think i have no more suggestion sorry :(

Hello,
I had already follow this tutorial at the beginning of my quest.
But I noticed something that I didn't think was important at the time... All the saved data has the keyword "export".
I added it to my code and everything is working perfectly now!
Thank you for your help

1 Answer

+3 votes
Best answer

Just turning my last comment as an answer...
If someone has this problem, add the "export" keyword to all the data you want to save and it should work

by (81 points)

understood, so the engine only save the data of the export var in the resource?
good to know! i dont think this is mentioned in any docs

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.