Multiplayer: How to get ID of server from client?

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

Hey all,

If I have the following code:

var peer = NetworkedMultiplayerENet.new()
peer.create_client(SERVER_IP, SERVER_PORT)
get_tree().network_peer = peer

How would I go about getting the network ID of the server I just connected to for the purposes of using rpc_id()? Or, alternatively, does using rpc() from a client only send to the server?

:bust_in_silhouette: Reply From: unlut

You should listen corresponding network signals.

func initialize_client():
    var peer = NetworkedMultiplayerENet.new()
    peer.create_client(SERVER_IP, SERVER_PORT)
    get_tree().network_peer = peer
    get_tree().multiplayer.connect("network_peer_connected", self, "_on_peer_connected_client")

func _on_peer_connected_client(id):
    print("Server network id:", id)

Wouldn’t this also trigger whenever another client connects to the server?

Also, upon further reading, it seems as if the server ID is always 1.

Bush2Tree | 2020-09-28 21:52

network_peer_connected signal is triggered for both clients and servers, but here I connected it in initialize_client function and to a specific function “_on_peer_connected_client” (this is why I added _client suffix to function name).

unlut | 2020-09-29 08:00

:bust_in_silhouette: Reply From: jonbonazza

You seem to have already found the answer on your own but I will stillprovide the answer here for posterity. The server always has network ID 1.

1 Like