Using Resource to save custom class not working

Godot Version

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


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.


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

func _on_button_pressed() -> void:
	var resInfo =
	resInfo.playerArray.append(, "a"))
	resInfo.playerArray.append(, "b"))
	resInfo.playerArray.append(, "c")), "res://abc.tres")

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()))
		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://" id="1_7vu0o"]
[ext_resource type="Script" path="res://" 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"

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.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:


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


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.

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

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