Using Resource to save custom class not working

Godot Version

Godot v4.3.beta1.official[a4f2ea91a]
Godot v4.2.2 stable

Question

I like to save custom class in the resource as an array but some how its not working for me. Can u guys look into my code and tell me whats wrong with it. Or Godot does not support custom class array it does save the data but while reading it does not work.

Code

extends Resource
class_name PLayerInfo
@export var life: int = 100
@export var name: String = ""
func _init(life: int, name: String) -> void:
	self.life = life
	self.name = name
extends Resource
class_name ResInfo
@export var playerArray: Array[PLayerInfo] = []
extends Control

func _on_button_pressed() -> void:
	var resInfo = ResInfo.new()
	resInfo.playerArray.append(PLayerInfo.new(1, "a"))
	resInfo.playerArray.append(PLayerInfo.new(2, "b"))
	resInfo.playerArray.append(PLayerInfo.new(3, "c"))
	ResourceSaver.save(resInfo, "res://abc.tres")
	print("Saved")

func _on_button_2_pressed() -> void:
	var data = ResourceLoader.load("res://abc.tres", "", ResourceLoader.CACHE_MODE_IGNORE)
	if data && data is ResInfo:
		print("Array Size::", str((data as ResInfo).playerArray.size()))
	else:
		print("File not found")

Log data:


Saved <--- this one after saving data
Array Size::0 <-- this one does not load data array size:0

abc.tres file

[gd_resource type="Resource" script_class="ResInfo" load_steps=6 format=3]

[ext_resource type="Script" path="res://PlayerInfo.gd" id="1_7vu0o"]
[ext_resource type="Script" path="res://ResInfo.gd" id="2_2l7g2"]

[sub_resource type="Resource" id="Resource_fxfkl"]
script = ExtResource("1_7vu0o")
life = 1
name = "a"

[sub_resource type="Resource" id="Resource_a03ln"]
script = ExtResource("1_7vu0o")
life = 2
name = "b"

[sub_resource type="Resource" id="Resource_7b11r"]
script = ExtResource("1_7vu0o")
life = 3
name = "c"

[resource]
script = ExtResource("2_2l7g2")
playerArray = Array[ExtResource("1_7vu0o")]([SubResource("Resource_fxfkl"), SubResource("Resource_a03ln"), SubResource("Resource_7b11r")])

I tried

ResourceLoader.load("res://abc.tres")
 ResourceLoader.load("res://abc.tres", "", ResourceLoader.CACHE_MODE_REPLACE)

Any suggestion what am i doing wrong, any suggestion would be helpful.

I think you might not need to define the _init() method the way you are in PLayerInfo (you could probably just delete it). I don’t think that’s the source of the error but it is something that might be problematic.

It looks like from the docs here: File system — Godot Engine (stable) documentation in English res is a read-only path, so you might want to try using:

user://abc.tres

As your target location

I don’t know if either of those things will actually fix it, but you can give it a try!

1 Like

I skimmed rapidly over the code and I noticed that your use of the class name is a bit weird. Probably a typo, but this might be problematic.

class_name = PLayerInfo

Notice the capital PL

path=“res://PlayerInfo.gd”

I’m wondering if the script is also having the capital letters or not, which might bring your problem.

I haven’t dabbled with saving data yet, so my knowledge on this is very limited, but that typo might become a problem later if it isn’t right now.

1 Like

Whenever you define _init(...) each of the arguments MUST have a default value else the engine will fail to parse the data.
Eg:

func _init(arg1: int = 0, arg2: String = ""):
2 Likes

The problem was i had to pass default parameter in the _init or not use _init at all.

2 Likes