Connect to another server while already be the host of one

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

Hello!

So I want to make a DB and I was trying to connect to another server (DB), from another server I’ll call the game server. I first tried connecting to DB from the main game server script, which worked, but the client couldn’t connect to the game server. So I moved it to another script and made it a single making sure to name the script the same as the node on DB. But doing that made them not communicate. Also made sure to change the port

any idea how to fix this?

:bust_in_silhouette: Reply From: bigstinky

It sounds to me like you are trying to make the game server a server and a client at the same time. You have the right idea by making a separate singleton in the game server for the communication with the database, but you also need to define a custom multiplayer API for that singleton, because the default one is already being used by the game server client.
The singleton would look something like this for the game server

var network: ENetMultiplayerPeer = ENetMultiplayerPeer.new()
var database_api: MultiplayerAPI = MultiplayerAPI.create_default_interface()
var DEFAULT_ADDRESS: String = "127.0.0.1"
var DEFAULT_PORT: int = 54545

func _process(_delta: float) -> void:
	if database_api.has_multiplayer_peer():
		database_api.poll()


func connect_to_server() -> void:
	network.create_client(DEFAULT_ADDRESS, DEFAULT_PORT)
	# replaces default multiplayer API for this node
    get_tree().set_multiplayer(database_api, self.get_path()) 
	database_api.multiplayer_peer = network
	
	database_api.connected_to_server.connect(_on_connected_to_server)
	database_api.connection_failed.connect(_on_connection_failed)

and on the database server you would create a server with the default multiplayer API like you normally would in a singleton of the same name