Is there any benefit to not doubling up on signals?

Godot Version

Godot 4.5

Question

Is there any benefit to running:

func a():
 var peer = multiplayer.peer_connected
 print(multiplayer.peer_connected)
 b(peer)

func b(peer):
 print(peer)

Over:

func a():
 var peer = multiplayer.peer_connected
 print(multiplayer.peer_connected)

func b():
 var peer = multiplayer.peer_connected
 print(multiplayer.peer_connected)

So essentially, is there any performance gain or style guide on not doubling up on signals?

Nothing gets “doubled” there. You just assign a reference to the signal object to a local variable.

1 Like

Can be faster if you create a variable and in the ready fill it, And add param to the function

1 Like

I don’t even understand what this is supposed to achieve, other than making your code less readable.

In any case, this is a micro-optimization, which are not worth it. Always profile and measure first.

2 Likes

So none of that code does anything except print:

SceneMultiplayer::[signal]peer_connected

Which will do nothing, because it doesn’t actually tell you if the peer is connected - it’s literally just telling you the name of the signal. That’s not how you use signals. You cannot assign them to variables and do anything with them. (Other than potentially obfuscate your code.)

You’re not doubling up on signals.

If you wanted to get something useful out of that signal you’d do something like this:

func _ready() -> void:
	multiplayer.peer_connected.connect(_on_player_connected)


func _on_player_connected(id: int) -> void:
	_register_player.rpc_id(id, player_info)


@rpc("any_peer", "reliable")
func _register_player(new_player_info):
	var new_player_id = multiplayer.get_remote_sender_id()
	players[new_player_id] = new_player_info
	player_connected.emit(new_player_id, new_player_info)

Answer

No.

Having said all that above, the answer to your initial question is: No. The interpreter is going to optimize your code better than you can. So whether you obfuscate your signals, or call one function with a reference it could have pulled in code (as @normalized pointed out)- it makes no difference. It’s going to run at the same speed. The only thing you’re doing in your code examples is make your code much harder for you (and anyone else working on your project) to read.

2 Likes