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 #)
This commit is contained in:
Andrew Tang 2022-08-01 02:48:32 +08:00
parent 3fbf2640da
commit db69386b6d
8 changed files with 71 additions and 8 deletions

View File

@ -0,0 +1,9 @@
extends Node
func instance_node(node: Object, parent: Object) -> Object:
var node_instance = node.instance()
parent.add_child(node_instance)
return node_instance

View File

@ -12,7 +12,7 @@ func _ready() -> void:
ip_address = IP.get_local_addresses()[3] ip_address = IP.get_local_addresses()[3]
for ip in IP.get_local_addresses(): for ip in IP.get_local_addresses():
if ip.begins_with("192.168."): if ip.begins_with("192.168.") and not ip.ends_with(".1"):
ip_address = ip ip_address = ip
get_tree().connect("connected_to_server", self, "_connected_to_server") get_tree().connect("connected_to_server", self, "_connected_to_server")

View File

@ -1,6 +1,9 @@
extends Control extends Control
var playerCounter = 1
var player = load("res://Player.tscn")
onready var multiplayer_config_ui = $Multiplayer_Configure onready var multiplayer_config_ui = $Multiplayer_Configure
onready var server_ip_address = $Multiplayer_Configure/Server_IP_Address onready var server_ip_address = $Multiplayer_Configure/Server_IP_Address
onready var device_ip_address = $CanvasLayer/Device_IP_Address onready var device_ip_address = $CanvasLayer/Device_IP_Address
@ -17,16 +20,34 @@ func _ready():
func _player_connected(id) -> void: func _player_connected(id) -> void:
print("Player " + str(id) + "connected") print("Player " + str(id) + "connected")
instance_player(id)
func _player_disconnected(id) -> void: func _player_disconnected(id) -> void:
print("Player " + str(id) + "disconnected") print("Player " + str(id) + "disconnected")
if Players.has_node(str(id)):
Players.get_node(str(id)).queue_free()
func _on_Create_Server_pressed(): func _on_Create_Server_pressed():
multiplayer.config_ui.hide() multiplayer_config_ui.hide()
Network._create_server() Network._create_server()
instance_player(get_tree().get_network_unique_id())
func _on_Join_Server_pressed(): func _on_Join_Server_pressed():
if server_ip_address.text != "": if server_ip_address.text != "":
multiplayer_config_ui.hide() multiplayer_config_ui.hide()
Network.ip_address = server_ip_address.text Network.ip_address = server_ip_address.text
Network._join_server() Network._join_server()
func _connected_to_server() -> void:
yield(get_tree().create_timer(0.1), "timeout")
instance_player(get_tree().get_network_unique_id())
func instance_player(id) -> void:
var player_instance = Global.instance_node(player, Players)
player_instance.name = str(id)
var player_name = "Player " + str(playerCounter)
player_instance._set_name(player_name)
player_instance._set_name_position(playerCounter)
player_instance.set_network_master(id)
playerCounter += 1

View File

@ -0,0 +1,7 @@
extends Control
func _set_name(name) -> void:
$Name.text = name
func _set_name_position(counterVal) -> void:
$Name.rect_position.y = -225 + (50 * counterVal)

View File

@ -0,0 +1,26 @@
[gd_scene load_steps=2 format=2]
[ext_resource path="res://Player.gd" type="Script" id=1]
[node name="Player" type="Control"]
anchor_right = 1.0
anchor_bottom = 1.0
script = ExtResource( 1 )
[node name="Name" type="Label" parent="."]
anchor_right = 1.0
anchor_bottom = 1.0
margin_top = -175.0
margin_bottom = -175.0
text = "Name"
align = 1
valign = 1
[node name="Players" type="Label" parent="."]
anchor_right = 1.0
anchor_bottom = 1.0
margin_top = -225.0
margin_bottom = -225.0
text = "Players:"
align = 1
valign = 1

View File

@ -0,0 +1,3 @@
[gd_scene format=2]
[node name="Players" type="Node"]

View File

@ -1,6 +0,0 @@
[gd_scene load_steps=2 format=2]
[ext_resource path="res://Network.gd" type="Script" id=1]
[node name="Node" type="Node"]
script = ExtResource( 1 )

View File

@ -11,11 +11,14 @@ config_version=4
[application] [application]
config/name="Multiplayer Test" config/name="Multiplayer Test"
run/main_scene="res://Network_Setup.tscn"
config/icon="res://icon.png" config/icon="res://icon.png"
[autoload] [autoload]
Network="*res://Network.gd" Network="*res://Network.gd"
Global="*res://Global.gd"
Players="*res://Players.tscn"
[physics] [physics]