Skip to content

Instantly share code, notes, and snippets.

@Jummit
Created September 12, 2020 14:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Jummit/5ff353f043b710cc95ff791f7382c7fc to your computer and use it in GitHub Desktop.
Save Jummit/5ff353f043b710cc95ff791f7382c7fc to your computer and use it in GitHub Desktop.
Using custom multiplayer to connect to a dedicated server and register a hosted server
extends Node
# registered as an autload on both
# the dedicated server and on the player
var MY_IP
var MY_PORT
var is_player
const DEDICATED_SERVER_PORT = 1234
func _ready() -> void:
# this node is present on both the
# dedicated server and the players
# only create a custom multiplayer
# instance on the player
if not is_player:
return
# setup a custom multiplayer separate
# the global SceneTree multiplayer
custom_multiplayer = MultiplayerAPI.new()
custom_multiplayer.set_root_node(self)
var peer := NetworkedMultiplayerENet.new()
custom_multiplayer.network_peer = peer
# connect to the dedicated
# server be creating a client
peer.create_client(
"dedicated server address",
DEDICATED_SERVER_PORT)
# send the dedicated server an rpc
# to register a newly hosted server
rpc("register_server", MY_IP, MY_PORT)
func _process(_delta : float) -> void:
if custom_multiplayer.has_network_peer():
# custom multiplayer needs
# to be polled manually
custom_multiplayer.poll()
master func register_server(
address : String, port : int) -> void:
# registration on the dedicated server here
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment