Issues with saving and loading resources

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

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

Class RessourceA :

extends Resource

class_name ResourceA

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

Class RessourceB :

extends Resource

class_name ResourceB

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

func run():
	for i in range(10):
		var res =
		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 :

extends Control
var resource_b_preload = preload("res://")
func _on_SaveFSMButton_pressed():
	var result ="res://file.tres", resource_b_preload)
	if result != 0:
		print("Something goes wrong !")

File’s content :

[gd_resource type="GDScript" format=2]

script/source = "tool
extends Resource

class_name ResourceB

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

func run():
	for i in range(10):
		var res =
		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 result ="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/" type="Script" id=1]

script = ExtResource( 1 )

I tried to load my resource like this:

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

and like this :

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

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 :


But the file is still the same

Roshi | 2021-01-11 14:09

i think another error could be that you used, res) instead of, res)
ResourceSaver — Godot Engine (stable) documentation in English
I’m not 100% sure though, as i cant find any documentation on method

Andrea | 2021-01-11 14:29

Sorry, it was a typo… I’m already using
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():

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?

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

Andrea | 2021-01-11 16:24

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

:bust_in_silhouette: Reply From: Roshi

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

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

Andrea | 2021-01-12 10:16