YARD extension doesn't convert loaded Resource to my custom Resource

Godot Version

4.7 stable

Question

I installed the YARD extension to have multiple spreadsheets(Registries) of all items, weapons, character info etc. for an RPG game.

Code examples show that it is possible to grab your custom resource like this:

func load_from_registry():
	const data: Registry = preload('res://data/registry/characters.tres')
	var player: Character = data.load_entry(&'player')

Straight from docs

But instead of giving me a Charactertype objectload_entry()returns a Resource

If you worked with YARD before, how did you pull your custom resources from registry into your code?

Forgot to add a screenshot with an error that I get:

Most likely because, as far as I can tell, this addon will give you the base Resource, and you are expected to cast it yourself.

func load_from_registry():
	const data: Registry = preload('res://data/registry/characters.tres')
	var playerResource: Resource = data.load_entry(&'player')
    var player: Character = playerResource as Character

Can you post your character.gd file?

Thanks, I didn’t know about the “as” keyword

For some reason my resource turns into null after casting it

Here is the test script I wrote:

extends Node

func _ready():
	var all_ids: Array[StringName] = DialogueInfo.characters.get_all_string_ids()
	for c in all_ids:
		var chara_resource = DialogueInfo.characters.load_entry(c)
		assert(chara_resource != null, 'Resource is null')
		var chara = chara_resource as Character
		assert(chara != null, 'Character is null after conversion')

The error I get after this check:

Assertion failed: Character is null after conversion

Can we see your character code?

class_name Character extends Resource

enum Sex {Male, Female}

@export var id: String
@export var name: String
@export var sex: Sex
@export var profile: AtlasTexture
@export var color_hex: String
@export var color: Color


func _init(id, name, sex, profile, color_hex, color) -> void:
	id = id
	name = name
	sex = sex
	profile = profile
	color_hex = color_hex
	color = color

This is the .gd file, the rest of it is just setters for variables.

[gd_resource type="Resource" script_class="Character" format=3 uid="uid://r0x3whvendxs"]

[ext_resource type="Script" uid="uid://whwj43s0rdue" path="res://scripts/classes/character.gd" id="1_1bckb"]

[sub_resource type="AtlasTexture" id="AtlasTexture_4e4m6"]

[resource]
script = ExtResource("1_1bckb")
id = "char3"
name = "Моргана"
sex = 1
profile = SubResource("AtlasTexture_4e4m6")
color_hex = "91db69"
color = Color(0.5686275, 0.85882354, 0.4117647, 1)
metadata/_custom_type_script = "uid://whwj43s0rdue"

And this is an example of a .tres file saved by the YARD extension

I don’t know much about this extension but it might be better to ask on their discussion forums if they have any. I don’t know the inner workings of it unfortunately, and documentation seems to be lacking.

Overriding _init is a pretty bad idea in general, is it required for the plugin you are using? Try removing that function override otherwise.

Seems like this was the problem, everything works now. Thanks for the tip!