+1 vote

I created a project where I instantiate some domino pieces, position them on the screen and set the appropriate texture.

I added two pieces to the scene manually and instantiated dynamically all the combination to see if they work.

Everything worked fine until I replaced my local var sprite: Sprite = $Sprite with a onready var sprite: Sprite = $Sprite

Not event initializing it in the func _ready() worked.

The two pieces I added manually worked regardless, but the dynamic ones return null for the sprite when using load("res://Piece.tscn").instance()

Here's the project: https://drive.google.com/file/d/1ymk5LyABpRFvuuS2O23NGG67xw9Enz6C/view?usp=sharing

in Engine by (16 points)

1 Answer

+1 vote
Best answer

You're calling set_piece before you call add_child, i.e. before the node's _ready-function is called and it's onready-vars are initialised, so le_sprite indeed isnull. Simply reverse the order of both calls and your problem should be fixed:

piece.set_piece(n1, n2)
by (10,582 points)
selected by
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.