bcirpggithubmirror/Phase2/Godot ===(Game Code)===/Experiments/Andrew_Multiplayer_Test/Network.gd
Andrew Tang db69386b6d Test Lobby Working
Server creation works and others are able to join by entering the creator's IP. Shows a list of player names (currently only shows player #)
2022-08-01 02:48:32 +08:00

36 lines
936 B
GDScript

extends Node
const DEFAULT_PORT = 28960
const MAX_CLIENTS = 6
var server = null
var client = null
var ip_address = ""
func _ready() -> void:
ip_address = IP.get_local_addresses()[3]
for ip in IP.get_local_addresses():
if ip.begins_with("192.168.") and not ip.ends_with(".1"):
ip_address = ip
get_tree().connect("connected_to_server", self, "_connected_to_server")
get_tree().connect("server_disconnected", self, "_server_disconnected")
func _create_server() -> void:
server = NetworkedMultiplayerENet.new()
server.create_server(DEFAULT_PORT, MAX_CLIENTS)
get_tree().set_network_peer(server)
func _join_server() -> void:
client = NetworkedMultiplayerENet.new()
client.create_client(ip_address, DEFAULT_PORT)
get_tree().set_network_peer(client)
func _connected_to_server() -> void:
print("Successfully connected to the server")
func _server_disconnected() -> void:
print("Disconnected from the server")