Custom signals aren't triggerred

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

I have a hard time trying to communicate using custom signals. I have a group of Objects that I’m trying to communicate clicks to player.

Player.gd:
extends Sprite

func _on_object_clicked(obj):
    print("object clicked", obj)

=====
Object.gd:
extends Sprite
var player_ref = null
signal object_clicked

func connect_clicks_to_player(player):
    print("connecting ", self, " to player ", player)
    connect("object_clicked", player, "_on_object_clicked")
    #player_ref = player

func on_input(viewport: Viewport, event, shape_idx):
    if event is InputEventMouseButton and event.is_pressed():
        print("emiting signal", self)
        emit_signal("object_clicked")
        #player_ref._on_object_clicked()

I am calling connect_clicks_to_player in a singleton. I see connecting being printed, with correct nodes, emitting signal being printed, but not _on_object_clicked in Player. Player also claims in get_incoming_connections that this signal is connected.

I also tried moving the signal to the singleton class, following this answer, but it didn’t change anything.

At the same time, when I stored the reference to the player directly instead (uncommenting the two lines) code runs correctly. What am I doing wrong?

:bust_in_silhouette: Reply From: Mxt08

If that script is in the Singleton, from another script, access the object.gd, and then declare the connect_clicks_to_player(player) function in the_ready().

Object.gd is not a singleton. Objects are constructed dynamically. I have a separate Singleton class, where, after instantiating the Objects, I’m calling connect_clicks_to_player. As I wrote above, it confirms being called, but emitting the signal has no effect.

sygi | 2021-04-03 14:03

:bust_in_silhouette: Reply From: sygi

I don’t know why, but turning the engine on and off fixed it. ¯_(ツ)_/¯