Hello community,
I believe this is a bug but I was not able to isolate into an MRP to properly post on the issues tracker. As such I am posting here to see if anyone has knowledge facing this or similar issues to help create an MRP and report the issue.
I am programming a card game with drag and drop functionality.
I have a Card Resource which is programmed as follows:
class_name CardResource extends Resource
@export var id: String = ""
@export var texture: Texture2D
I have a Card Class which begins as follows. The following is a short snippet, but contains as far as I can tell, all of the relevant code.
class_name Card extends Node2D
### Initialization
@export var card_resource: CardResource:
set(value):
card_resource = value
$Sprite2D.texture = value.texture
func _ready():
print(card_resource)
As is, $Sprite2D.texture is not being set, and print(card_resource) evaluates to <null>.
However, if I change the code as follows:
class_name Card extends Node2D
### Initialization
@export var card_resource: Resource: # This line has changed, from the custom resource type to generic resource
set(value):
card_resource = value
$Sprite2D.texture = value.texture
func _ready():
print(card_resource)
Then the print correctly evaluates to the resource's UID, and a sprite is correctly set for the card.
This only happened after re-factoring out a large chunk of code.
Within that code, after reviewing this issue, I notice that the code I refactored into a different class contains the following snippet:
var card_registry: Dictionary = {
lumber = preload("res://Card/CardResource/lumber.tres"),
stone = preload("res://Card/CardResource/stone.tres"),
character = preload("res://Card/CardResource/character.tres"),
}
If I re-add that snippet, the version of the code using custom typing now works as normal.
However, this cannot be correct behaviour. The resources should be pre-loaded during compilation time by the editor, without me needing to preload them manually in the script - this is how they behave without the custom typing being declared!
Please advise if there is any way to formulate this into an MRP. When trying to reproduce this bug, I was not able to - perhaps something about refactoring out the preloading from the editor changed the behaviour of the compilation?
Apologies for any poor formatting, it is regrettable that this forum does not have a preview feature.
Thank you for looking into this.