The problem is that when the scene is instanced (e.g. PackedScene::instance
is called, which happens for all scenes before they are displayed), it would start setting all properties of the nodes... until it gets to your tool-scripted node, and attempts to set its property. At that point, neither _enter_tree
nor _ready
has occurred, which means that get_node
is still not able to do work, thus get_node("Sprite")
results in null.
"In theory, this sounds good," you say, "but what about in practice? How can this be fixed?"
Well, the fix is rather simple, you can just check has_node
's output, like so
if !has_node("Sprite"):
return
Another option would be to set a variable to true in _ready
, and calling all setgets right afterwards:
var ready_called = false
func _ready():
ready_called = true
set_newkeyname(keyname)
# Or `self.keyname = keyname`/`set("keyname", keyname)`
func set_newkeyname(newkeyname):
if ready_called:
# Do stuff...
There are a few other options, but I guess that is enough for an answer ;-)