0 votes
onready var red = get_node("red")
onready var green = get_node("green")
onready var blue = get_node("blue")

red, green and blue are children of the current node, yet they are null when get_node is called?

Editor tree


onready var _factory = load("res://scenes/blockfactory.tscn")
var factory = _factory.instance()
if factory == null:
    print("no factory!")
var block = factory.createRed(100, 100)
if block == null:
    print("no red")

_factory is fine, but createdRed fails


func createRed(x, y):
if red == null:
    print("no red")
         return null
    var block = red.duplicate()
    if block == null:
         print("no block")
         return null
    block.set_pos(Vector2(x, y))
    return block
asked Jan 13 in Engine by imekon (133 points)

1 Answer

0 votes
Best answer

onready runs after node is added to scene tree.
but you called createRed before it's added to scene tree.


answered Jan 13 by volzhs (6,425 points)
selected Jan 13 by imekon

I see... I forgot to add the instance of factory to the scene tree, once I do that it starts working.

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 How to use this Q&A? before posting your first questions.