0 votes

I'm working through the tutorial, trying to understand signals.
I have a parent who will receive a custom signal from a child and then print to console.

Currently, when I call Object.connect() at runtime in the parent, the child is _ready first and therefore emits its signal *before* the parent has a chance to connect a listener.
Therefore, the signal can't be processed and nothing happens.

However, if I use the UI and connect my custom signal using the Node's Signal connection interface, then the parent does connect before the child is _ready and processes the emitted signal.

Now, I could yield in the child and specifically wait for the parent's _ready function manually, but I'm wondering how I can write the code to mimic the timing of what happens when I connect the signal in the editor?


extends Node2D
func _ready():
  $Timer.connect("my_signal", self, "_on_Timer_my_signal")

func _on_Timer_my_signal():
  print("my signal made it!")

Child (node named Timer):

extends Timer
signal my_signal

func _ready():
  print("emitting signal")
Godot version 3.32
in Engine by (15 points)
edited by

2 Answers

+1 vote
Best answer

Try this for the child by using call_deferred:

extends Timer
signal my_signal

func _ready():
    print("emitting signal")
by (805 points)
selected by
0 votes

You may find it helpful to let the child node connect its signal to the parent node, rather than the other way around. Firstly, because the child's _ready() method will fire before the parent's and so avoid the problem you had, and secondly, it avoids the possibility that the child node may be changed or removed in the editor, or at runtime, and the parent would try to connect to a nonexistent child. You will never have the opposite problem, a child trying to connect a signal to a nonexistent parent, because if the parent were removed, it would take the child with it.

by (16 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.