From c84681fdfac7999c5fbcf15c4355f7ab7e7b943a Mon Sep 17 00:00:00 2001 From: Chris Bell Date: Sat, 28 Dec 2024 20:32:43 -0600 Subject: [PATCH] Added new methods to network manager --- .../core/networking/scripts/NetworkManager.gd | 37 +++++++++++++++---- .../core/player-controller/scripts/player.gd | 6 +-- assets/core/ships/ship_script.gd | 4 +- 3 files changed, 34 insertions(+), 13 deletions(-) diff --git a/assets/core/networking/scripts/NetworkManager.gd b/assets/core/networking/scripts/NetworkManager.gd index 86df267..4a0e0ca 100644 --- a/assets/core/networking/scripts/NetworkManager.gd +++ b/assets/core/networking/scripts/NetworkManager.gd @@ -17,6 +17,7 @@ var steam_id: int = 0 var steam_username: String = "" var avatar_texture_cache: Dictionary = {} var is_host: bool = false +var host_id: int var uuid_counter: int = 0 @@ -203,6 +204,7 @@ func host_lobby(): if lobby_id == 0: Steam.createLobby(lobby_type, lobby_members_max) is_host = true + host_id = steam_id else: printerr("Cannot host lobby, already in a lobby") @@ -211,12 +213,14 @@ func join_lobby(this_lobby_id: int): print("Attempting to join lobby: " + str(this_lobby_id)) lobby_members.clear() Steam.joinLobby(this_lobby_id) + host_id = Steam.getLobbyOwner(this_lobby_id) func leave_lobby(): if lobby_id != 0: Steam.leaveLobby(lobby_id) lobby_id = 0 + host_id = 0 for this_member in lobby_members: if this_member["steam_id"] != steam_id: @@ -270,8 +274,14 @@ func send_p2p_packet(target: int, packet_data: Dictionary) -> void: if this_member['steam_id'] != steam_id: Steam.sendP2PPacket(this_member['steam_id'], this_data, send_type, channel) #print("Sent packet to %s." % this_member['steam_name']) - - # Else send it to someone specific + # If sending a packet to everyone except the host + elif target == 1: + if lobby_members.size() > 1: + # Loop through all members that aren't the host + for this_member in lobby_members: + if this_member['steam_id'] != host_id: + Steam.sendP2PPacket(this_member['steam_id'], this_data, send_type, channel) + # Else send it to someone specific else: Steam.sendP2PPacket(target, this_data, send_type, channel) @@ -318,7 +328,6 @@ func read_p2p_packet() -> void: if "message" in readable_data and readable_data["message"] == "property_update": if "node_id" in readable_data and "property_name" in readable_data and "value" in readable_data: emit_signal("property_update_received", readable_data["node_id"], readable_data["property_name"], readable_data["value"]) - #print("Received property update packet for node %d: %s = %s" % [readable_data["node_id"], readable_data["property_name"], readable_data["value"]]) func register_node(node: Node) -> String: @@ -389,11 +398,23 @@ func _on_game_started(): level.add_child(player) -func sync_property(node_id: String, property_name: String, value: Variant): - var packet_data = {"message":"property_update", "node_id": node_id, "property_name":property_name, "value":value} - send_p2p_packet(0, packet_data) - +func sync_property_to_host(node_id: String, property_name: String, value: Variant): + var packet_data = {"message":"property_update", "node_id": node_id, "property_name": property_name, "value": value} + send_p2p_packet(host_id, packet_data) + func sync_property_to_peer(node_id: String, property_name: String, value: Variant, target_peer: int): var packet_data = {"message":"property_update", "node_id": node_id, "property_name": property_name, "value": value} - send_p2p_packet(target_peer, packet_data) + send_p2p_packet(target_peer, packet_data) + + +func sync_property_to_all(node_id: String, property_name: String, value: Variant): + var packet_data = {"message":"property_update", "node_id": node_id, "property_name":property_name, "value":value} + send_p2p_packet(0, packet_data) + + +func sync_property_to_all_except_host(node_id: String, property_name: String, value: Variant): + var packet_data = {"message":"property_update", "node_id": node_id, "property_name":property_name, "value":value} + send_p2p_packet(1, packet_data) + + diff --git a/assets/core/player-controller/scripts/player.gd b/assets/core/player-controller/scripts/player.gd index 254c4f0..002bebd 100644 --- a/assets/core/player-controller/scripts/player.gd +++ b/assets/core/player-controller/scripts/player.gd @@ -95,15 +95,15 @@ func _process(delta: float) -> void: # If the global_position has changed, notify the NetworkManager if previous_global_position != global_position: - NetworkManager.sync_property(network_uuid, "global_position", global_position) + NetworkManager.sync_property_to_all(network_uuid, "global_position", global_position) previous_global_position = global_position if previous_global_rotation != global_rotation: - NetworkManager.sync_property(network_uuid, "global_rotation", global_rotation) + NetworkManager.sync_property_to_all(network_uuid, "global_rotation", global_rotation) previous_global_rotation = global_rotation if previous_head_vert_rotation != neck.rotation: - NetworkManager.sync_property(network_uuid, "neck_rotation_sync", neck_rotation_sync) + NetworkManager.sync_property_to_all(network_uuid, "neck_rotation_sync", neck_rotation_sync) previous_global_rotation = neck.rotation diff --git a/assets/core/ships/ship_script.gd b/assets/core/ships/ship_script.gd index 3bd189d..b549ec2 100644 --- a/assets/core/ships/ship_script.gd +++ b/assets/core/ships/ship_script.gd @@ -94,12 +94,12 @@ func sync_ship_state(): if piloting_player and piloting_player.is_network_authority or NetworkManager.is_host: # Send updates only if the position has changed if global_position != previous_position: - NetworkManager.sync_property(network_uuid, "global_position", global_position) + NetworkManager.sync_property_to_all(network_uuid, "global_position", global_position) previous_position = global_position var current_rotation = global_transform.basis.get_rotation_quaternion() if current_rotation != previous_rotation: - NetworkManager.sync_property(network_uuid, "global_rotation", current_rotation) + NetworkManager.sync_property_to_all(network_uuid, "global_rotation", current_rotation) previous_rotation = current_rotation # Interpolation for non-piloting players (who only receive the updates)