0 votes
I preloaded my custom scene, right after instantiating  it by code, I tried to set the properties of its instance. There's no problem if I just set the root node's properties.However, if I tend to set the child's node properties, it would not working and give me an error. After debugging, I made the assumption that I had problem with getting the referrence of the child node. Because I use "onready" to get the child node. It's before the root node go to ready stage that I try to set the properties. My ref would be null. So I tried to use *_defferd() function. It worked. But I wonder, is there a more elegant way to deal with this. Can I just get child node's referrence in a early stage. 

I do not konw when the child node is instantiated. Or maybe the child node's instance has not been created at all when I try to access it.

Godot version v3.4.4.stable.official [419e713a2]
in Engine by (14 points)

1 Answer

0 votes

I see You opened a double issue for your problem

I have to repeat and explain some more
First You preload scene.
at this moment all default values of introduced variables are set, except onready vars
Next You call instance()
at this moment init() is called
Finally you call add_child()
at this moment ready() is called and onready vars values are set

So after all this happened You can safely change properties of your instance. I suspect You have a conflicting code, setting property in ready() with some exported value and manually after instantiation using another value. I understand this is why You have to call_deferred() - to wait for ready function, so it won't reset your manual value.

You just need to change this design :). Set properties for every objects consequently in one place, You just need to decide.

by (7,301 points)
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.