The function set() is executed before _ready()

Yes, @export vars are set immediately when the scene is instantiated (pretty much right when _init() is called on the node), but the _ready() function and @onready vars aren’t processed until after the node is added to the tree.

So, you’re simply going to need to assume textureRect could be null, and then make a _ready() function which sets the texture.

Something like this should work:

@export var ITEM: InventoryItem: set = _set_item

@onready var textureRect: TextureRect = $Texture

func _ready():
	_update_texture()

func _set_item(value: InventoryItem):
	ITEM = value
	_update_texture()

func _update_texture():
	if not is_instance_valid(textureRect):
		return
	if ITEM != null:
		textureRect.texture = ITEM.image
	else:
		textureRect.texture = null
2 Likes