+2 votes

Hi,

Some of my nodes dependent from other nodes. _ready is called but the dependent node is not initiated (not ready).

Is there any standard way to have a completed hierarchy so the dependent structures can setup?

I use ugly things like:

func _ready():
    yield(get_tree().create_timer(0.1),"timeout")
    superDependentCallsAndSetups()

Thank you,
David

in Engine by (321 points)

What's the relationship between the node with that script and the dependent node? Is it parent or child?

Child modifies the behavior of the parent.

I'm not sure, but isn't this a job for 'signals'?
I know that every node in your tree emits a signal on ready. You should be able to trigger some function in your mother-node, whenever specific ready-signals have been received.

My solution turned out like this.

1, I added all of the dependent children nodes to a group "finalready".
2, I moved the scripts in the _ready() to a new method final
ready().
3, I call the callgroup("finalready") from the scene root in the _ready() function.

The result is that. I initialize all the dependent nodes after the whole scene structure is ready.

Is there any better solution?

The best solution would be to get rid of the dependency, and use dependency injection instead, as described in https://docs.godotengine.org/en/3.1/getting_started/workflow/best_practices/scene_organization.html#how-to-build-relationships-effectively

But if this doesn't work for whatever reason, your call_group solution looks fine.

Please log in or register to answer this question.

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.