Reconnecting a signal after disconecting it

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

I have a turn manager that alternates turns between the player and his opponent; the player connects the signal to a button when his turns begins, and disconnects it when his turn ends. The problem is that the button doesn’t fire after being reconnected.
So I’m wondering if it’s normal that a signal cannot be reconnected after having been disconnected, or if I’m doing something wrong

The code I’m using is:

func turn_start():
  button.connect("pressed", self, "_on_button_pressed")

func turn_end():
  button.disconnect("pressed", self, "_on_button_pressed")

func _on_button_pressed():
  #do stuff
  turn_end()
:bust_in_silhouette: Reply From: MaaaxiKing

First, the normal signal name is pressed. Yet, you do not reconnect the button with this code, you just disconnect it. A signal can be connected after it has been disconnected. After button.disconnect("pressed", self, "_on_button_pressed") you must do button.connect("pressed", self, "_on_button_pressed") but that has no sense.

Thanks, I forgot to mention that the turn manager calls player.start_turn() to reconnect the button, yet pressing the button doesn’t trigger the callback if it’s pressed after being reconnected

Tamarindo94 | 2020-12-27 21:29

:bust_in_silhouette: Reply From: BytePlayer

While I don’t understand why it isn’t working per se, I think a cleaner way of doing it would be through an event_bus.

It could have a global on_pressed signal that routes it appropriately depending on what turn it is. There an article about it here: http://www.drunkenhyena.com/godot/godot_recipes/event_bus_tutorial.html

That’s instructive, thanks!

Tamarindo94 | 2020-12-28 14:30