0 votes

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?

in Engine by (174 points)

2 Answers

+4 votes
Best answer

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.

by (100 points)
selected by
+1 vote

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)
by (136 points)

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.

networkpeerconnected signal is triggered for both clients and servers, but here I connected it in initializeclient function and to a specific function "onpeerconnected_client" (this is why I added _client suffix to function name).

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 Frequently asked questions and 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 [email protected] with your username.