From 0ba178da8a53acb5aeeb187d49323c86418bd382 Mon Sep 17 00:00:00 2001 From: Chris Bell Date: Tue, 17 Dec 2024 21:15:20 -0600 Subject: [PATCH 1/4] Adding lobby list --- assets/core/networking/scenes/lobby.tscn | 23 ++++++-- .../core/networking/scripts/NetworkManager.gd | 11 +++- assets/core/networking/scripts/lobby.gd | 57 ++++++++++++++++--- 3 files changed, 76 insertions(+), 15 deletions(-) diff --git a/assets/core/networking/scenes/lobby.tscn b/assets/core/networking/scenes/lobby.tscn index e339666..30ddcbf 100644 --- a/assets/core/networking/scenes/lobby.tscn +++ b/assets/core/networking/scenes/lobby.tscn @@ -3,7 +3,7 @@ [ext_resource type="Script" path="res://assets/core/networking/scripts/lobby.gd" id="1_o4fbq"] [ext_resource type="PackedScene" uid="uid://biryul3n6thlw" path="res://assets/core/networking/scenes/user_box_prefab.tscn" id="2_dpthk"] -[node name="Lobby" type="Control" node_paths=PackedStringArray("host_button", "leave_button", "start_button", "user_list_box")] +[node name="Lobby" type="Control" node_paths=PackedStringArray("host_button", "leave_button", "start_button", "show_lobbies_button", "user_list_box", "lobby_list_box")] layout_mode = 3 anchors_preset = 15 anchor_right = 1.0 @@ -14,7 +14,9 @@ script = ExtResource("1_o4fbq") host_button = NodePath("Panel/HBoxContainer/Buttons/Host") leave_button = NodePath("Panel/HBoxContainer/Buttons/Leave") start_button = NodePath("Panel/HBoxContainer/Buttons/Start") +show_lobbies_button = NodePath("Panel/HBoxContainer/Buttons/ShowLobbies") user_list_box = NodePath("Panel/HBoxContainer/PlayerList") +lobby_list_box = NodePath("Panel/HBoxContainer/LobbyList") user_box_prefab = ExtResource("2_dpthk") [node name="Panel" type="Panel" parent="."] @@ -44,16 +46,25 @@ layout_mode = 2 size_flags_vertical = 4 text = "Host Lobby" -[node name="Leave" type="Button" parent="Panel/HBoxContainer/Buttons"] -layout_mode = 2 -disabled = true -text = "Leave Lobby" - [node name="Start" type="Button" parent="Panel/HBoxContainer/Buttons"] layout_mode = 2 disabled = true text = "Start" +[node name="ShowLobbies" type="Button" parent="Panel/HBoxContainer/Buttons"] +layout_mode = 2 +text = "Show Lobbies" + +[node name="Leave" type="Button" parent="Panel/HBoxContainer/Buttons"] +layout_mode = 2 +disabled = true +text = "Leave Lobby" + [node name="PlayerList" type="VBoxContainer" parent="Panel/HBoxContainer"] +visible = false layout_mode = 2 size_flags_horizontal = 3 + +[node name="LobbyList" type="VBoxContainer" parent="Panel/HBoxContainer"] +visible = false +layout_mode = 2 diff --git a/assets/core/networking/scripts/NetworkManager.gd b/assets/core/networking/scripts/NetworkManager.gd index 5e1692f..25764e2 100644 --- a/assets/core/networking/scripts/NetworkManager.gd +++ b/assets/core/networking/scripts/NetworkManager.gd @@ -28,6 +28,7 @@ signal user_joined_lobby(user_id: int) signal user_left_lobby(user_id: int) signal host_left_lobby() signal on_game_started() +signal on_lobbies_received(these_lobbies: Array) signal property_update_received(node_id: int, property_name: String, value: Variant) @@ -143,8 +144,8 @@ func _on_lobby_joined(this_lobby_id: int, _permissions: int, _locked: bool, resp print("Failed to join this chat room: %s" % fail_reason) -func _on_lobby_match_list(): - pass +func _on_lobby_match_list(these_lobbies: Array) -> void: + on_lobbies_received.emit(these_lobbies) func _on_lobby_message(): @@ -224,6 +225,12 @@ func leave_lobby(): lobby_members.clear() print("Left lobby.") + + +func get_lobbies(): + Steam.addRequestLobbyListDistanceFilter(Steam.LOBBY_DISTANCE_FILTER_WORLDWIDE) + Steam.requestLobbyList() + func get_lobby_members(): lobby_members.clear() diff --git a/assets/core/networking/scripts/lobby.gd b/assets/core/networking/scripts/lobby.gd index fdf3180..164d9c7 100644 --- a/assets/core/networking/scripts/lobby.gd +++ b/assets/core/networking/scripts/lobby.gd @@ -3,7 +3,9 @@ extends Control @export var host_button: Button @export var leave_button: Button @export var start_button: Button +@export var show_lobbies_button: Button @export var user_list_box: VBoxContainer +@export var lobby_list_box: VBoxContainer @export var user_box_prefab: PackedScene var added_users = [] @@ -19,8 +21,9 @@ func _ready() -> void: NetworkManager.user_joined_lobby.connect(_on_user_joined_lobby) NetworkManager.user_left_lobby.connect(_on_user_left_lobby) NetworkManager.on_game_started.connect(_on_game_started) + NetworkManager.lobbies_received.connect(_on_lobbies_received) - GameConsole.register_command(Command.new("update", update, [], "Updates the lobby UI")) + GameConsole.register_command(Command.new("update", update_ui, [], "Updates the lobby UI")) func _on_host(): @@ -29,9 +32,32 @@ func _on_host(): func _on_leave(): NetworkManager.leave_lobby() - added_users.clear() - for child in user_list_box.get_children(): - child.queue_free() + reset_ui() + + +func _on_show_lobbies(): + NetworkManager.get_lobbies() + + +func _on_lobbies_received(these_lobbies: Array): + lobby_list_box.visible = true + for this_lobby in these_lobbies: + # Pull lobby data from Steam, these are specific to our example + var lobby_name: String = Steam.getLobbyData(this_lobby, "name") + var lobby_mode: String = Steam.getLobbyData(this_lobby, "mode") + + # Get the current number of members + var lobby_num_members: int = Steam.getNumLobbyMembers(this_lobby) + + # Create a button for the lobby + var lobby_button: Button = Button.new() + lobby_button.set_text("Lobby %s: %s [%s] - %s Player(s)" % [this_lobby, lobby_name, lobby_mode, lobby_num_members]) + lobby_button.set_size(Vector2(800, 50)) + lobby_button.set_name("lobby_%s" % this_lobby) + lobby_button.connect("pressed", Callable(self, "join_lobby").bind(this_lobby)) + + # Add the new lobby to the list + lobby_list_box.add_child(lobby_button) func _on_lobby_created(lobby_id: int): @@ -39,23 +65,26 @@ func _on_lobby_created(lobby_id: int): host_button.disabled = true leave_button.disabled = false start_button.disabled = false + user_list_box.visible = true func _on_lobby_joined(lobby_id: int): host_button.disabled = true leave_button.disabled = false + user_list_box.visible = true + lobby_list_box.visible = false func _on_user_joined_lobby(user_id: int): if user_id in added_users: return # User already added, skip added_users.append(user_id) - update() + update_ui() func _on_user_left_lobby(user_id: int): added_users.erase(user_id) - update() + update_ui() func _on_start(): @@ -67,7 +96,21 @@ func _on_game_started(): visible = false -func update() -> void: +func reset_ui(): + host_button.disabled = false + leave_button.disabled = true + start_button.disabled = true + user_list_box.visible = false + lobby_list_box.visible = false + added_users.clear() + for child in user_list_box.get_children(): + child.queue_free() + + for child in lobby_list_box.get_children(): + child.queue_free() + + +func update_ui() -> void: for child in user_list_box.get_children(): child.queue_free() -- 2.45.2 From be7898a555c7eb8ddcc8678f452b480acbd94f15 Mon Sep 17 00:00:00 2001 From: Chris Bell Date: Tue, 17 Dec 2024 21:20:04 -0600 Subject: [PATCH 2/4] Connected the button --- assets/core/networking/scripts/lobby.gd | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/assets/core/networking/scripts/lobby.gd b/assets/core/networking/scripts/lobby.gd index 164d9c7..04961de 100644 --- a/assets/core/networking/scripts/lobby.gd +++ b/assets/core/networking/scripts/lobby.gd @@ -15,13 +15,14 @@ func _ready() -> void: host_button.pressed.connect(_on_host) leave_button.pressed.connect(_on_leave) start_button.pressed.connect(_on_start) + show_lobbies_button.pressed.connect(_on_show_lobbies) NetworkManager.lobby_created.connect(_on_lobby_created) NetworkManager.lobby_joined.connect(_on_lobby_joined) NetworkManager.user_joined_lobby.connect(_on_user_joined_lobby) NetworkManager.user_left_lobby.connect(_on_user_left_lobby) NetworkManager.on_game_started.connect(_on_game_started) - NetworkManager.lobbies_received.connect(_on_lobbies_received) + NetworkManager.on_lobbies_received.connect(_on_lobbies_received) GameConsole.register_command(Command.new("update", update_ui, [], "Updates the lobby UI")) @@ -36,11 +37,11 @@ func _on_leave(): func _on_show_lobbies(): + lobby_list_box.visible = true NetworkManager.get_lobbies() func _on_lobbies_received(these_lobbies: Array): - lobby_list_box.visible = true for this_lobby in these_lobbies: # Pull lobby data from Steam, these are specific to our example var lobby_name: String = Steam.getLobbyData(this_lobby, "name") -- 2.45.2 From e90d9444d428544a5397d428829a1dc20ec3d9ea Mon Sep 17 00:00:00 2001 From: Chris Bell Date: Tue, 17 Dec 2024 21:22:13 -0600 Subject: [PATCH 3/4] Actually join lobby and disable show button if hosting --- assets/core/networking/scripts/lobby.gd | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/assets/core/networking/scripts/lobby.gd b/assets/core/networking/scripts/lobby.gd index 04961de..3828def 100644 --- a/assets/core/networking/scripts/lobby.gd +++ b/assets/core/networking/scripts/lobby.gd @@ -29,6 +29,7 @@ func _ready() -> void: func _on_host(): NetworkManager.host_lobby() + show_lobbies_button.disabled = true func _on_leave(): @@ -60,6 +61,11 @@ func _on_lobbies_received(these_lobbies: Array): # Add the new lobby to the list lobby_list_box.add_child(lobby_button) + + +func join_lobby(lobby_id: int): + NetworkManager.join_lobby(lobby_id) + func _on_lobby_created(lobby_id: int): print("Lobby created") -- 2.45.2 From a90121de9320dc882284e72f7df808668bcf67e1 Mon Sep 17 00:00:00 2001 From: Chris Bell Date: Tue, 17 Dec 2024 21:25:43 -0600 Subject: [PATCH 4/4] Fix show button not showing back up when leaving --- assets/core/networking/scripts/lobby.gd | 1 + 1 file changed, 1 insertion(+) diff --git a/assets/core/networking/scripts/lobby.gd b/assets/core/networking/scripts/lobby.gd index 3828def..2587a40 100644 --- a/assets/core/networking/scripts/lobby.gd +++ b/assets/core/networking/scripts/lobby.gd @@ -109,6 +109,7 @@ func reset_ui(): start_button.disabled = true user_list_box.visible = false lobby_list_box.visible = false + show_lobbies_button.disabled = false added_users.clear() for child in user_list_box.get_children(): child.queue_free() -- 2.45.2