• Godot Help
  • Custom Resource has no information after load

I have a custom Resource class PowerUp defined as follows:
(PowerUp.gd)

extends Resource
class_name PowerUp
## A resource that describes a game Item

## The user-facing name of this item
@export var name: String
## This item's identifier or, if empty, uses the resource filename's basename
@export var id: StringName
## How this item should present itself
@export var sprite: SpriteFrames
## Whether this item can be stored if the player already has a power-ups
@export var is_storable: bool = true

func _init(p_name: String, p_sprite: SpriteFrames = null, p_is_storable: bool = true, p_id: StringName = &""):
    ## Omitted

I defined a resource in the inspector and saved it as super_mushroom.res. I can load it just fine using both load and preload, but is not an instance of PowerUp.
Here's an example of my singleton class that loads all of the resources and adds them to a lookup table:
(Items.gd)

extends Node
## This class is marked as Autoload

# I added this mostly out of desperation, but online resources say is is not necessary because `PowerUp` is registered
var PowerUp = preload("res://items/PowerUp.gd")

static var ITEMS = {}
static var POWER_UPS: Array[PowerUp] = [
    preload("res://items/super_mushroom.tres") as PowerUp
]

static func _add_powerup(pu: PowerUp):
    ITEMS[pu.id] = pu

func _ready():
    for pu in POWER_UPS:
                ## this line crashes the editor. See message below VVVVVVVVVVVV
        _add_powerup(pu)

I get the following error message:
Invalid type in function '_add_powerup' in base 'Node (Items.gd)'. The Object-derived class of argument 1 (Resource) is not a subclass of the expected argument class.

I tried a log of things, but nothing worked and all I get from loading those resources are blank resources, as if they aren't inheriting from my PowerUp.

Here's an example of a PowerUp, my super_mushroom:

Ok, so the problem was that I forgot to add default parameters for all of the fields in _init

Since those are all default values, they can be ommited and the final PowerUp class looks like this

extends Resource
class_name PowerUp
## A resource that describes a game Item

## The user-facing name of this item
@export var name: String = ""
## This item's identifier or, if empty, uses the resource filename's basename
@export var id: StringName = &""
## How this item should present itself
@export var sprite: SpriteFrames = null
## Whether this item can be stored if the player already has a power-ups
@export var is_storable: bool = true