Signals: what am I doing wrong?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Trash Panda

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!

:bust_in_silhouette: Reply From: DodoIta

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 :slight_smile:

You are quite correct- thank you! :slight_smile:

Trash Panda | 2021-06-23 17:28