extends Control # Default game server port. Can be any number between 1024 and 49151. # Not on the list of registered or common ports as of November 2020: # https://en.wikipedia.org/wiki/List_of_TCP_and_UDP_port_numbers const DEFAULT_PORT = 8910 onready var address = $Address onready var host_button = $HostButton onready var join_button = $JoinButton onready var status_ok = $StatusOk onready var status_fail = $StatusFail onready var port_forward_label = $PortForward onready var find_public_ip_button = $FindPublicIP var peer = null func _ready(): # Connect all the callbacks related to networking. get_tree().connect("network_peer_connected", self, "_player_connected") get_tree().connect("network_peer_disconnected", self, "_player_disconnected") get_tree().connect("connected_to_server", self, "_connected_ok") get_tree().connect("connection_failed", self, "_connected_fail") get_tree().connect("server_disconnected", self, "_server_disconnected") #### Network callbacks from SceneTree #### # Callback from SceneTree. func _player_connected(_id): # Someone connected, start the game! var pong = load("res://pong.tscn").instance() # Connect deferred so we can safely erase it from the callback. pong.connect("game_finished", self, "_end_game", [], CONNECT_DEFERRED) get_tree().get_root().add_child(pong) hide() func _player_disconnected(_id): if get_tree().is_network_server(): _end_game("Client disconnected") else: _end_game("Server disconnected") # Callback from SceneTree, only for clients (not server). func _connected_ok(): pass # This function is not needed for this project. # Callback from SceneTree, only for clients (not server). func _connected_fail(): _set_status("Couldn't connect", false) get_tree().set_network_peer(null) # Remove peer. host_button.set_disabled(false) join_button.set_disabled(false) func _server_disconnected(): _end_game("Server disconnected") ##### Game creation functions ###### func _end_game(with_error = ""): if has_node("/root/Pong"): # Erase immediately, otherwise network might show # errors (this is why we connected deferred above). get_node("/root/Pong").free() show() get_tree().set_network_peer(null) # Remove peer. host_button.set_disabled(false) join_button.set_disabled(false) _set_status(with_error, false) func _set_status(text, isok): # Simple way to show status. if isok: status_ok.set_text(text) status_fail.set_text("") else: status_ok.set_text("") status_fail.set_text(text) func _on_host_pressed(): peer = NetworkedMultiplayerENet.new() peer.set_compression_mode(NetworkedMultiplayerENet.COMPRESS_RANGE_CODER) var err = peer.create_server(DEFAULT_PORT, 1) # Maximum of 1 peer, since it's a 2-player game. if err != OK: # Is another server running? _set_status("Can't host, address in use.",false) return get_tree().set_network_peer(peer) host_button.set_disabled(true) join_button.set_disabled(true) _set_status("Waiting for player...", true) # Only show hosting instructions when relevant. port_forward_label.visible = true find_public_ip_button.visible = true func _on_join_pressed(): var ip = address.get_text() if not ip.is_valid_ip_address(): _set_status("IP address is invalid", false) return peer = NetworkedMultiplayerENet.new() peer.set_compression_mode(NetworkedMultiplayerENet.COMPRESS_RANGE_CODER) peer.create_client(ip, DEFAULT_PORT) get_tree().set_network_peer(peer) _set_status("Connecting...", true) func _on_find_public_ip_pressed(): OS.shell_open("https://icanhazip.com/")