From 50439bc8e0ef9525d3a42085cd449c1a48f35026 Mon Sep 17 00:00:00 2001 From: itdominator <1itdominator@gmail.com> Date: Sat, 8 Aug 2026 01:22:54 -0500 Subject: [PATCH] Moving some of game to use MessageBus --- globals/message_bus.gd | 40 ++++++++++++++++------- scenes/data_bridge/lobby_data.gd | 19 +---------- scenes/data_bridge/multiplayer_data.gd | 13 +++++--- scenes/individual_parts/cue/cue.gd | 12 +++---- scenes/screens/lobby_screen/lobby.gd | 17 +++++----- scenes/screens/lobby_screen/lobby_base.gd | 2 +- scenes/screens/lobby_screen/lobby_rpc.gd | 34 ------------------- 7 files changed, 54 insertions(+), 83 deletions(-) diff --git a/globals/message_bus.gd b/globals/message_bus.gd index c09d669..f716f05 100644 --- a/globals/message_bus.gd +++ b/globals/message_bus.gd @@ -5,36 +5,52 @@ var message_types: Dictionary[String, Array] = {} func subscribe(id: String, callback: Callable) -> void: - var listeners: Array = message_types.get(id) - - if not listeners: + if not message_types.has(id): message_types.set(id, Array()) - listeners = message_types.get(id) - listeners.append(callback) + message_types.get(id).append(callback) func emit(id: String, data: Variant) -> void: emit_propagation(id, data) +# TODO: Need to actually validate client request. Also, message bus is more +# about server talking to clients than clients to server. I.E, reducing +# the number of needed @rpc method decorator bindings. @rpc("any_peer", "call_remote", "reliable") -func emit_request(id: String, data: Variant) -> void: +func emit_request(id: String, data: Variant, expand: bool = false) -> void: + if not multiplayer.is_server(): return + var listeners: Array = message_types.get(id) if not listeners: push_error("'%s' not an existing message type to send...", [id]) return - emit_propagation(id, data) - for peer_id in multiplayer.get_peers(): - emit_propagation.rpc_id(peer_id, id, data) + emit_propagation(id, data, expand) -@rpc("authority", "call_remote", "reliable") -func emit_propagation(id: String, data: Variant) -> void: +@rpc("authority", "call_local", "reliable") +func emit_propagation(id: String, data: Variant, expand: bool = false) -> void: var listeners: Array = message_types.get(id) if not listeners: push_error("'%s' not an existing message type to send...", [id]) return for callback: Callable in listeners: - callback.call(data) + if not expand: + callback.call(data) + else: + callback.callv(data) + +@rpc("authority", "call_remote", "reliable") +func emit_propagation_clients_only(id: String, data: Variant, expand: bool = false) -> void: + var listeners: Array = message_types.get(id) + if not listeners: + push_error("'%s' not an existing message type to send...", [id]) + return + + for callback: Callable in listeners: + if not expand: + callback.call(data) + else: + callback.callv(data) diff --git a/scenes/data_bridge/lobby_data.gd b/scenes/data_bridge/lobby_data.gd index ce20c8c..d63d7cf 100644 --- a/scenes/data_bridge/lobby_data.gd +++ b/scenes/data_bridge/lobby_data.gd @@ -16,16 +16,12 @@ func connect_to_game() -> void: Globals.multiplayer_data.wait_for_connection_close() # NOTE: Join target game server + # TODO: Don't use libby client... Use a created game client. lobby_ui.client.start_client( Globals.game_data.game_address, Globals.game_data.game_port ) -@rpc("authority", "call_local", "reliable") -func load_game_scene(match_entry: Dictionary, is_player_1: bool) -> void: - push_warning("Client load game scene...") - lobby_ui.rpc_signals.match_list_entry_load_match.emit(match_entry, is_player_1) - @rpc("authority", "reliable") func go_back_to_match_screen() -> void: if multiplayer.is_server(): return @@ -35,16 +31,3 @@ func go_back_to_match_screen() -> void: Globals.game_state.set_game_scene( "res://scenes/screens/lobby_screen/lobby.tscn" ) - -# NOTE: Needed for hash check to pass between client and server but isn't used by client... -@rpc("any_peer", "reliable") -func match_list_add_entry(_match_entry: Dictionary) -> void: - pass - -@rpc("any_peer", "reliable") -func match_list_remove_entry(_match_id: String) -> void: - pass - -@rpc("any_peer", "reliable") -func match_list_join_entry(_match_id: String) -> void: - pass diff --git a/scenes/data_bridge/multiplayer_data.gd b/scenes/data_bridge/multiplayer_data.gd index 13d098d..ff7c67b 100644 --- a/scenes/data_bridge/multiplayer_data.gd +++ b/scenes/data_bridge/multiplayer_data.gd @@ -30,13 +30,18 @@ func init_match(): rpc("set_active_player_id", Globals.game_data.client1) - Globals.lobby_data.rpc_id( + MessageBus.emit_propagation_clients_only.rpc_id( Globals.game_data.client1, - "load_game_scene", match_entry, true + "match_list_entry_load_match", + [match_entry, true], + true ) - Globals.lobby_data.rpc_id( + + MessageBus.emit_propagation_clients_only.rpc_id( Globals.game_data.client2, - "load_game_scene", match_entry, false + "match_list_entry_load_match", + [match_entry, false], + true ) func wait_for_connection() -> void: diff --git a/scenes/individual_parts/cue/cue.gd b/scenes/individual_parts/cue/cue.gd index e1d8f2a..e0772c6 100644 --- a/scenes/individual_parts/cue/cue.gd +++ b/scenes/individual_parts/cue/cue.gd @@ -19,9 +19,9 @@ func _input(event: InputEvent) -> void: rpc_input("aim_release") else: if event.is_action_pressed("aim_pulse"): - self.rpc_id(1, "rpc_input", "aim_pulse") + rpc_id(1, "rpc_input", "aim_pulse") elif event.is_action_released("aim_release"): - self.rpc_id(1, "rpc_input", "aim_release") + rpc_id(1, "rpc_input", "aim_release") func _process(delta: float) -> void: @@ -44,10 +44,10 @@ func _process(delta: float) -> void: rpc_process("aim_decline", delta) else: if Input.is_action_pressed("aim_rotate_left"): - self.rpc_id(1, "rpc_process", "aim_rotate_left", delta) + rpc_id(1, "rpc_process", "aim_rotate_left", delta) elif Input.is_action_pressed("aim_rotate_right"): - self.rpc_id(1, "rpc_process", "aim_rotate_right", delta) + rpc_id(1, "rpc_process", "aim_rotate_right", delta) elif Input.is_action_pressed("aim_incline"): - self.rpc_id(1, "rpc_process", "aim_incline", delta) + rpc_id(1, "rpc_process", "aim_incline", delta) elif Input.is_action_pressed("aim_decline"): - self.rpc_id(1, "rpc_process", "aim_decline", delta) + rpc_id(1, "rpc_process", "aim_decline", delta) diff --git a/scenes/screens/lobby_screen/lobby.gd b/scenes/screens/lobby_screen/lobby.gd index f212713..926c88f 100644 --- a/scenes/screens/lobby_screen/lobby.gd +++ b/scenes/screens/lobby_screen/lobby.gd @@ -13,18 +13,19 @@ func _exit_tree() -> void: client.close_connection() func setup_signals() -> void: - rpc_signals.match_list_entries.connect(_match_list_entries) - rpc_signals.match_list_entry_activated.connect(_on_match_list_entry_activated) - rpc_signals.match_list_entry_added.connect(_on_match_list_entry_added) - rpc_signals.match_list_entry_removed.connect(_on_match_list_entry_removed) - rpc_signals.match_list_entry_load_match.connect(_on_match_list_entry_load_match) + MessageBus.subscribe("receive_match_list", _on_receive_match_list) + MessageBus.subscribe("match_list_activate_entry", _on_match_list_entry_activated) + MessageBus.subscribe("match_list_add_entry", _on_match_list_entry_added) + MessageBus.subscribe("match_list_remove_entry", _on_match_list_entry_removed) + MessageBus.subscribe("match_list_entry_load_match", _on_match_list_entry_load_match) + func _server_host_started() -> void: host_connect_bttn.visible = false # NOTE: 'matches_list' can be empty but so long as we get the call from the # server we know we are connected and thus hide respective UI elements. -func _match_list_entries(matches_list: Array) -> void: +func _on_receive_match_list(matches_list: Array) -> void: push_warning("Client Joined Host: Full Match List Recieved...\n", matches_list) match_create_vbox.visible = true @@ -94,7 +95,7 @@ func _create_match_entry(match_entry: Dictionary) -> void: container.join_bttn.visible = false func _join_match(match_id: String) -> void: - Globals.lobby_data.rpc_id(1, "match_list_join_entry", match_id) + MessageBus.emit_request.rpc_id(1, "match_list_join_entry", match_id) func _delete_match(match_id: String) -> void: - Globals.lobby_data.rpc_id(1, "match_list_remove_entry", match_id) + MessageBus.emit_request.rpc_id(1, "match_list_remove_entry", match_id) diff --git a/scenes/screens/lobby_screen/lobby_base.gd b/scenes/screens/lobby_screen/lobby_base.gd index 9624596..47b7184 100644 --- a/scenes/screens/lobby_screen/lobby_base.gd +++ b/scenes/screens/lobby_screen/lobby_base.gd @@ -52,7 +52,7 @@ func _on_match_create_bttn_pressed() -> void: "type": active_mode } - Globals.lobby_data.rpc_id(1, "match_list_add_entry", match_entry) + MessageBus.emit_request.rpc_id(1, "match_list_add_entry", match_entry) func _on_mode_bttn_pressed(button: Button) -> void: active_mode = button.text.remove_chars("-").to_lower() diff --git a/scenes/screens/lobby_screen/lobby_rpc.gd b/scenes/screens/lobby_screen/lobby_rpc.gd index 22549ce..61510e1 100644 --- a/scenes/screens/lobby_screen/lobby_rpc.gd +++ b/scenes/screens/lobby_screen/lobby_rpc.gd @@ -1,35 +1 @@ extends Node - - -signal match_list_entries(match_list: Array) -signal match_list_entry_activated(match_id: String) -signal match_list_entry_added(match_entry: Dictionary) -signal match_list_entry_removed(match_id: String) -signal match_list_entry_load_match( - match_entry: Dictionary, is_player_1: bool -) - - -@rpc("authority", "call_local", "reliable") -func match_list_add_entry(match_entry: Dictionary) -> void: - push_warning("Client received new match entry...") - match_list_entry_added.emit(match_entry) - -@rpc("authority", "call_local", "reliable") -func match_list_remove_entry(match_id: String) -> void: - push_warning("Client received delete match entry...") - match_list_entry_removed.emit(match_id) - -@rpc("authority", "reliable") -func receive_match_list(match_list: Array) -> void: - push_warning("Client received match list...") - match_list_entries.emit(match_list) - -@rpc("authority", "reliable") -func match_list_activate_entry(match_id: String) -> void: - push_warning("Client set active match entry...") - match_list_entry_activated.emit(match_id) - -func load_game_scene(match_entry: Dictionary, is_player_1: bool) -> void: - push_warning("Client load game scene...") - match_list_entry_load_match.emit(match_entry, is_player_1)