From db69386b6d2f12adf284b45a3f84111bbfeb2b0b Mon Sep 17 00:00:00 2001 From: Andrew Tang Date: Mon, 1 Aug 2022 02:48:32 +0800 Subject: [PATCH] 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 #) --- .../Andrew_Multiplayer_Test/Global.gd | 9 +++++++ .../Andrew_Multiplayer_Test/Network.gd | 2 +- .../Andrew_Multiplayer_Test/Network_Setup.gd | 23 +++++++++++++++- .../Andrew_Multiplayer_Test/Player.gd | 7 +++++ .../Andrew_Multiplayer_Test/Player.tscn | 26 +++++++++++++++++++ .../Andrew_Multiplayer_Test/Players.tscn | 3 +++ .../Andrew_Multiplayer_Test/Test.tscn | 6 ----- .../Andrew_Multiplayer_Test/project.godot | 3 +++ 8 files changed, 71 insertions(+), 8 deletions(-) create mode 100644 Phase2/Godot ===(Game Code)===/Experiments/Andrew_Multiplayer_Test/Global.gd create mode 100644 Phase2/Godot ===(Game Code)===/Experiments/Andrew_Multiplayer_Test/Player.gd create mode 100644 Phase2/Godot ===(Game Code)===/Experiments/Andrew_Multiplayer_Test/Player.tscn create mode 100644 Phase2/Godot ===(Game Code)===/Experiments/Andrew_Multiplayer_Test/Players.tscn delete mode 100644 Phase2/Godot ===(Game Code)===/Experiments/Andrew_Multiplayer_Test/Test.tscn diff --git a/Phase2/Godot ===(Game Code)===/Experiments/Andrew_Multiplayer_Test/Global.gd b/Phase2/Godot ===(Game Code)===/Experiments/Andrew_Multiplayer_Test/Global.gd new file mode 100644 index 0000000..6c826ed --- /dev/null +++ b/Phase2/Godot ===(Game Code)===/Experiments/Andrew_Multiplayer_Test/Global.gd @@ -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 + + diff --git a/Phase2/Godot ===(Game Code)===/Experiments/Andrew_Multiplayer_Test/Network.gd b/Phase2/Godot ===(Game Code)===/Experiments/Andrew_Multiplayer_Test/Network.gd index 1fbc43d..052b427 100644 --- a/Phase2/Godot ===(Game Code)===/Experiments/Andrew_Multiplayer_Test/Network.gd +++ b/Phase2/Godot ===(Game Code)===/Experiments/Andrew_Multiplayer_Test/Network.gd @@ -12,7 +12,7 @@ func _ready() -> void: ip_address = IP.get_local_addresses()[3] 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 get_tree().connect("connected_to_server", self, "_connected_to_server") diff --git a/Phase2/Godot ===(Game Code)===/Experiments/Andrew_Multiplayer_Test/Network_Setup.gd b/Phase2/Godot ===(Game Code)===/Experiments/Andrew_Multiplayer_Test/Network_Setup.gd index 426b3d1..f1bee1b 100644 --- a/Phase2/Godot ===(Game Code)===/Experiments/Andrew_Multiplayer_Test/Network_Setup.gd +++ b/Phase2/Godot ===(Game Code)===/Experiments/Andrew_Multiplayer_Test/Network_Setup.gd @@ -1,6 +1,9 @@ extends Control +var playerCounter = 1 +var player = load("res://Player.tscn") + onready var multiplayer_config_ui = $Multiplayer_Configure onready var server_ip_address = $Multiplayer_Configure/Server_IP_Address onready var device_ip_address = $CanvasLayer/Device_IP_Address @@ -17,16 +20,34 @@ func _ready(): func _player_connected(id) -> void: print("Player " + str(id) + "connected") + instance_player(id) func _player_disconnected(id) -> void: print("Player " + str(id) + "disconnected") + + if Players.has_node(str(id)): + Players.get_node(str(id)).queue_free() func _on_Create_Server_pressed(): - multiplayer.config_ui.hide() + multiplayer_config_ui.hide() Network._create_server() + instance_player(get_tree().get_network_unique_id()) func _on_Join_Server_pressed(): if server_ip_address.text != "": multiplayer_config_ui.hide() Network.ip_address = server_ip_address.text 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 diff --git a/Phase2/Godot ===(Game Code)===/Experiments/Andrew_Multiplayer_Test/Player.gd b/Phase2/Godot ===(Game Code)===/Experiments/Andrew_Multiplayer_Test/Player.gd new file mode 100644 index 0000000..d47114b --- /dev/null +++ b/Phase2/Godot ===(Game Code)===/Experiments/Andrew_Multiplayer_Test/Player.gd @@ -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) diff --git a/Phase2/Godot ===(Game Code)===/Experiments/Andrew_Multiplayer_Test/Player.tscn b/Phase2/Godot ===(Game Code)===/Experiments/Andrew_Multiplayer_Test/Player.tscn new file mode 100644 index 0000000..ce42f2b --- /dev/null +++ b/Phase2/Godot ===(Game Code)===/Experiments/Andrew_Multiplayer_Test/Player.tscn @@ -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 diff --git a/Phase2/Godot ===(Game Code)===/Experiments/Andrew_Multiplayer_Test/Players.tscn b/Phase2/Godot ===(Game Code)===/Experiments/Andrew_Multiplayer_Test/Players.tscn new file mode 100644 index 0000000..2683f83 --- /dev/null +++ b/Phase2/Godot ===(Game Code)===/Experiments/Andrew_Multiplayer_Test/Players.tscn @@ -0,0 +1,3 @@ +[gd_scene format=2] + +[node name="Players" type="Node"] diff --git a/Phase2/Godot ===(Game Code)===/Experiments/Andrew_Multiplayer_Test/Test.tscn b/Phase2/Godot ===(Game Code)===/Experiments/Andrew_Multiplayer_Test/Test.tscn deleted file mode 100644 index 45a4ba7..0000000 --- a/Phase2/Godot ===(Game Code)===/Experiments/Andrew_Multiplayer_Test/Test.tscn +++ /dev/null @@ -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 ) diff --git a/Phase2/Godot ===(Game Code)===/Experiments/Andrew_Multiplayer_Test/project.godot b/Phase2/Godot ===(Game Code)===/Experiments/Andrew_Multiplayer_Test/project.godot index aa2113f..7546d2a 100644 --- a/Phase2/Godot ===(Game Code)===/Experiments/Andrew_Multiplayer_Test/project.godot +++ b/Phase2/Godot ===(Game Code)===/Experiments/Andrew_Multiplayer_Test/project.godot @@ -11,11 +11,14 @@ config_version=4 [application] config/name="Multiplayer Test" +run/main_scene="res://Network_Setup.tscn" config/icon="res://icon.png" [autoload] Network="*res://Network.gd" +Global="*res://Global.gd" +Players="*res://Players.tscn" [physics]