Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | Roshi |
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
not super expert on the topic, but resource_b_preload 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)
?
Andrea | 2021-01-11 13:11
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
Roshi | 2021-01-11 13:23
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?
Andrea | 2021-01-11 13:47
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
Roshi | 2021-01-11 14:09
i think another error could be that you used Resource.save(path, res)
instead of ResourceSaver.save(path, res)
ResourceSaver — Godot Engine (stable) documentation in English
I’m not 100% sure though, as i cant find any documentation on Resource.save() method
Andrea | 2021-01-11 14:29
Sorry, it was a typo… I’m already using ResourceSaver.save.
I fixed my first message
Roshi | 2021-01-11 14:50
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?
Andrea | 2021-01-11 15:34
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
Roshi | 2021-01-11 15:56
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
Andrea | 2021-01-11 16:24
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
Roshi | 2021-01-12 09:58