This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
0 votes

Hi!

I'm a new Godot developer, and I'm trying out the signal functionality.

I created a singleton (called Singleton, creative I know) that generates some instances of a SignalListener that simply connect/react to a specific signal from the Singleton.

The code is as follows:

Singleton:

extends Node

signal my_signal

export var signal_name: String = "my_signal"
export var n_objects: int = 10
export var current_id: int = 0

const LISTENER: PackedScene = preload("res://SignalListener.tscn")
var first_time: bool = true

func _ready():
    print("Creating objects ...")
    for i in range(n_objects):
        add_child(LISTENER.instance())

    print("Children:")
    for child in get_children():
        print("  " + child.name)

    print("Emitting '%s' from _ready() ..." % (signal_name))
    emit_signal(signal_name)

    print("Completed singleton _ready()")

func _process(delta):
    if first_time:
        print("Emitting '%s' from _process() ..." % (signal_name))
        emit_signal(signal_name)
        first_time = false

SignalListener:

extends Spatial

func _ready():
    set_name("object %d" % Singleton.current_id)
    Singleton.current_id += 1
    connect(Singleton.signal_name, self, "_on_Singleton_signal")
    print("Created: %s" % name)

func _on_Singleton_signal():
    print("Signal received: %s" % name)

When I run the system, I ge the following output:

--- Debugging process started ---
Godot Engine v3.3.2.stable.official - https://godotengine.org
OpenGL ES 3.0 Renderer: Intel Iris Pro OpenGL Engine
OpenGL ES Batching: ON

Registered camera FaceTime HD Camera with id 1 position 0 at index 0
Creating objects ...
Created: object 0
Created: object 1
Created: object 2
Created: object 3
Created: object 4
Created: object 5
Created: object 6
Created: object 7
Created: object 8
Created: object 9
Children:
  object 0
  object 1
  object 2
  object 3
  object 4
  object 5
  object 6
  object 7
  object 8
  object 9
Emitting 'my_signal' from _ready() ...
Completed singleton _ready()
Emitting 'my_signal' from _process() ...
--- Debugging process stopped ---

... so the Singleton is created, and generates the 10 SignalListeners which are successfully added as child nodes. However, the SignalListeners don't seem to receive the signal emitted by Singleton in either _ready() or the first _process().

Can anyone tell me where I'm going wrong?

Thanks!

Godot version 3.3.2.stable.official
in Engine by (15 points)

1 Answer

+1 vote
Best answer

Hi!
I was about to suggest you to try connecting the signal directly in the Singleton script, like this:

func _ready():
    print("Creating objects ...")
    for i in range(n_objects):
        var listener = LISTENER.instance()
        add_child(listener)
        self.connect(signal_name, listener, "_on_Singleton_signal")

But then I realized you misused the connect method, because by typing connect(Singleton.signal_name, self, "_on_Singleton_signal") you are trying to connect a signal declared inside SignalListener (i.e. self).
The correct way would be Singleton.connect(Singleton.signal_name, self, "_on_Singleton_signal") if you're typing this inside SignalListener.

I think this should fix it :)

by (569 points)
selected by

You are quite correct- thank you! :)

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.