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

main.gd:

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

_factory is fine, but createdRed fails

blockfactory.gd:

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 (181 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.

http://docs.godotengine.org/en/stable/learning/scripting/gdscript/gdscript_basics.html?highlight=onready#keywords

answered Jan 13 by volzhs (7,179 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.