Created
September 12, 2020 14:29
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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