+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

asked Jun 4, 2019 in Engine by DavidPeterWorks (190 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 How to use this Q&A? before posting your first questions.