How can I connect with multiple servers from Client

Godot Version

4.2.2

Question

Hi, I’m quite new to Godot, and trying to do some research over game server-client relationship.

Specifically, for a client, I’d like to connect with normal remote server, AND local server(on same device, distinct process) at the same time. (Note that this is for research purpose, not for developing real game)

Client should be able to send RPC for both servers.

For now, I’m testing with Multiplayer Bomber example. I tried to connect with my local server in this way :

    # This is for remote server
	peer = ENetMultiplayerPeer.new()
	peer.create_client(ip, DEFAULT_PORT)
	multiplayer.set_multiplayer_peer(peer)

    # This is for local server
    # I've declared "local_server_multiplayer" as global variable
	local_server_peer = ENetMultiplayerPeer.new()
	local_server_peer.create_client("127.0.0.1", LOCAL_SERVER_PORT)
	
	local_server_multiplayer.set_multiplayer_peer(local_peer)

and tried to send RPC with local_server_multiplayer

gamestate.local_server_multiplayer.rpc(1, gamestate, "register_player", ["test_player_name"])

Which does not work at all - it seems like local server does not react at all…

How can I achieve to connect multiple servers? Any suggestions?

the multiplayer class can only hold one peer, so i dont think you can achieve this with ENetPeers

1 Like

Correct.
and that’s why I declared another multiplayer object local_server_multiplayer, but don’t know why it’s not working…

Well to help you we would need to know how that local_server_mutliplayer is set up

Nothing special, I just tried to mimics the codes for already existing multiplayer.

var local_server_multiplayer = MultiplayerAPI.create_default_interface()

The only difference is that I explicitly declared local_server_multiplayer as an global variable.

There might be problems with the scene tree then i guess. Look at the docs:

Thanks for suggestion. After look into some docs, I think I get the problem. Here is my understanding so far, correct me if I’m wrong :

  1. Multiplayer instance cannot be in effect unless it’s bind to SceneTree
    => This is the reason of my failure on my post.

  2. Each SceneTree can be assigned single Multiplayer
    => I was confused as docs mentions “branches”, but it seems like Multiplayer can only be assigned on root of SceneTree

It is possible to override the MultiplayerAPI instance used by specific tree branches by calling the SceneTree.set_multiplayer method, effectively allowing to run both client and server in the same scene.

So, I guess in order to enable multiple Multiplayer instance, i.e. connect with multiple servers, I need to add another SceneTree on my sample game OR modify Godot engine code of SceneTree so that it can contains multiple instance of Multiplayer

Modifying engine code looks very challenging, but one of my goal is to minimizing code change of existing game client. I’m not sure take which approach…

1 Like