Custom Resource: fields recognized by the editor but not by scripts

Godot Version

4.2.1

Question

Why are the fields of my custom resource unknown in scripts but show up in the editor?
I assign instances of my custom resource “Item” to export variables in the editor.
But when trying to access their fields, e.g. field “type” I get the error
Invalid get index 'type' (on base: 'Resource').

class_name Item
extends Resource

@export var type = 0
@export var levels = {}

func _init(_type, _levels):
	type   = _type
	levels = _levels

Instance of custom resource saved as 2100.tres

[gd_resource type="Resource" script_class="Item" load_steps=2 format=3 uid="uid://bkt04gatgdrdb"]

[ext_resource type="Script" path="res://types/item.gd" id="1_mpm58"]

[resource]
script = ExtResource("1_mpm58")
type = 2100
levels = {}

Don’t use required parameters in Object._init() or the Resource will fail to load.

1 Like

Many thanks. I changed the _init function like this:

func _init(_type=0, _levels={}):
	type   = _type
	levels = _levels

I overlooked that hint in the tutorial since it worked somehow in the editor
Resources

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.