I have a scene with a weapon resource that has an image and a sprite child node

class_name Player extends CharacterBody2D
@onready var weapon_sprite: Sprite2D = $Weapon

@export var weapon: Weapon:
	set(value):
		print(value.name)
		weapon_sprite.texture = value.texture
		weapon = value

The thing is that because I start my player with an instance of weapon assigned, the property setter assigns the weapon texture to the sprite texture before its ready.
Is there any way to get around this or fix it?

  • xyz replied to this.
  • Set the texture in _ready. In the setter guard the texture assignment with if is_node_ready():

    xyz because weapon is an export variable I already have it set in the editor, it gets called as soon as I load the scene

    • xyz replied to this.

      Set the texture in _ready. In the setter guard the texture assignment with if is_node_ready():

        PenguinJuice311 You can call is_inside_tree() or is_node_rady() in the setter and if it returns false skip doing anything that expect fully set up scene tree. Other than that you can avoid doing tree dependent stuff in the setter altogether. Add another function that does it and call it later when all relevant nodes are ready.