0 votes

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?

Godot version 3.1.1-stable
in Engine by (68 points)

2 Answers

0 votes
Best answer

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

by (68 points)
0 votes

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().

by (353 points)

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.

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 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 webmaster@godotengine.org with your username.