GAME DEV BLOG
-
Coffee Mayhem #01
Today I was trying to load a texture for a pick up. I couldn’t figure out why it wasn’t working.
At first, I thought it was because the engine wouldn’t let me use a sprite saved inside a custom Resource (which I thought was very weird).
Then I tried loading the resource path for said sprite, still no dice. I tried saving it a variable before loading it, I tried typing it out – still not luck.
I’m going to save myself (and others) the trouble and get to the point. The way I fixed it was by doing the following
func _ready(): $PickupSprite.texture = pickup.spritePrior attempts were done using:
@onready var sprite = $PickupSprite.texture func _ready(): sprite = pickup.spriteWhich should work.. I think? I’m guessing there’s some referencing issue between “onready” and “_ready()”. I’m still looking into it to better understand the issue.
Anyway, I wanted to document this in case I ran into the same issue.. or if anyone else did. When I find a proper answer, I’ll edit this!
EDIT: It appears accessing the properties of a node with @onready doesn’t work. Everything worked as intended once I changed the code to this:
@onready var sprite = $PickupSprite func _ready(): sprite.texture = pickup.sprite