Godot Version
v4.5.1 for Linux x86_64
Question
Was torn between posting this in help and the general resource area. I don’t have an issue so much as I would like to understand what’s up with this behavior.
I have a custom resource called InventoryItem.gd. It appears like so:
class_name InventoryItem
extends Resource
@export var display_name: String
@export var icon: Texture2D
@export_multiline var description: String
@export_file_path var drop_instance_path
@export var equip_instance: PackedScene
@export var use_instance: PackedScene
I have a few inherited scenes like torch.tres and coins.tres that define these properties. When I try to instance them in a scene like so:
var item: InventoryItem = preload("res://items/inventory/foo.tres")
print(item == null)
>>> true
Preload does appear to produce a valid, empty, generic resource, but casting it to InventoryItem causes it to go null, as happens when doing the implied type:
var item = preload("res://items/inventory/foo.tres")
I’m surprised to see there’s no resource path there.
On the flip side, using load instead of preload appears to populate things correctly and can generate an InventoryItem.
For comedy value, the following also appears to work, too, but seems like a bad idea:
var item = InventoryItem.new()
item.resource_path = "res://items/inventory/doorkey.tres"
I found this potentially related issue: Error accessing custom resource caused by order of preload · Issue #87019 · godotengine/godot · GitHub but it’s not raising a get_index problem so I think that’s perhaps tangential.
Is it just the case that custom resources always need to be loaded with ‘load’ instead of preload? I didn’t see that in the docs.
