Connecting same signal on same node multiple times

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

What happens or what is the side effect (if any) to connect the same signal to the same node and callable, multiple times?

For example:

var nodes = get_tree().get_nodes_in_group("group-name")
	for n in nodes:
		n.mouse_entered.connect(_handleMouseOverFx.bind(n))

Will the callable be “re-assigned” to the node if already connected? Does it work the same way as assigning the same value to the same variable multiple times?

Or, will the node have multiple references to the same Callable?

Thank you all!

1 Like
:bust_in_silhouette: Reply From: zhyrin

This is the sort of thing you can test in a sandbox project in 2 minutes.

Godot 3.x threw an error if you tried to connect to an already existing function, I don’t see why it would be any different in 4.0.

Correction to my own comment below… @zhyrin is right. The engine will throw an error when trying to connect the same signal more than once. It will not crash the game, but will output the error to the ERRORS tab, as in:

E 0:00:01:0036   Lobby.gd:291 @ _connectControlsFxSignal(): Signal 'mouse_entered' is already connected to given callable 'Control(Lobby.gd)::_playMouseOverFx' in that object.

Right… I made a simple test.

Added a new button, a couple functions and connected them like so:

func _ready():
	$Button.mouse_entered.connect(A)
	$Button.mouse_entered.connect(A)
	$Button.mouse_entered.connect(B)
    
func A():
	print("a")
	
func B():
	print("b")

I’ve got no errors. And the output was:

a
b

So, it seems like Godot4 just ignore if the same function is connected more than once for the same signal.

simonlee | 2023-03-24 23:20

That’s great to hear.
Personally I don’t think it should be an error, only a warning.

zhyrin | 2023-03-27 09:31

1 Like