Child _ready() before Parent

Godot Version

Godot_v4.0.2-stable_win64

Question

I need help solving a child/parent relationship. As far I understand, children scripts are loaded before the Parent script.

I have a _ready() in the Parent script with an array [0,1,2,3] to influence behaviour of grandchildren.

Scene Tree: Parent – Child 1 – Grandchildren upto 4 – Child 2 – Grandchildren upto 4

Using a match statement in the grandchildren _ready() to behave a certain way based on the Parent variable and shuffled array.

How can I achieve this (I’m a beginner)?

To solve this issue, you can use a signal in the Parent script and connect it to the children scripts. Here’s a step-by-step solution:

  1. In the Parent script, declare a signal at the top of the script:
signal ready_signal
  1. In the _ready() function of the Parent script, emit the signal after setting up the array:
func _ready():
    var array = [0, 1, 2, 3]
    #... other setup code...
    emit_signal("ready_signal")
  1. In the Child script, create a function that will be called when the signal is emitted from the Parent script. This function can be called anything, e.g., _on_Parent_ready().
func _on_Parent_ready():
    # This function will be called when the Parent script emits the ready_signal
    # You can access the Parent script's variables and functions from here
    pass
  1. In the Child script, connect the ready_signal from the Parent script to the _on_Parent_ready() function using the connect function:
func _ready():
    get_parent().connect("ready_signal", self, "_on_Parent_ready")
  1. Now, in the Grandchildren script, you can access the Parent script’s variables and functions through the Child script. You can use the match statement as needed.

By using a signal, you ensure that the Child script waits for the Parent script to finish setting up before executing its own code. This way, you can access the Parent script’s variables and functions safely.

Remember to adjust the script names and function names according to your actual scene hierarchy.

1 Like

Thank you for your detailed answer.
I simply had to change get_parent().connect(“ready_signal”, self, “_on_Parent_ready”) to
get_parent().connect(“ready_signal”, self._on_Parent_ready) I guess depending on the Godot version.

Many thanks

You don’t need a seperate signal. Each node emits a ready signal when ready.
And you don’t need to connect it. You can use await to stop further code execution until the ready signal gets emitted:

func _ready() -> void:
	await get_parent().ready
2 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.