Added new methods to network manager

This commit is contained in:
Chris Bell 2024-12-28 20:32:43 -06:00
parent d225145927
commit c84681fdfa
3 changed files with 34 additions and 13 deletions

View File

@ -17,6 +17,7 @@ var steam_id: int = 0
var steam_username: String = "" var steam_username: String = ""
var avatar_texture_cache: Dictionary = {} var avatar_texture_cache: Dictionary = {}
var is_host: bool = false var is_host: bool = false
var host_id: int
var uuid_counter: int = 0 var uuid_counter: int = 0
@ -203,6 +204,7 @@ func host_lobby():
if lobby_id == 0: if lobby_id == 0:
Steam.createLobby(lobby_type, lobby_members_max) Steam.createLobby(lobby_type, lobby_members_max)
is_host = true is_host = true
host_id = steam_id
else: else:
printerr("Cannot host lobby, already in a lobby") 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)) print("Attempting to join lobby: " + str(this_lobby_id))
lobby_members.clear() lobby_members.clear()
Steam.joinLobby(this_lobby_id) Steam.joinLobby(this_lobby_id)
host_id = Steam.getLobbyOwner(this_lobby_id)
func leave_lobby(): func leave_lobby():
if lobby_id != 0: if lobby_id != 0:
Steam.leaveLobby(lobby_id) Steam.leaveLobby(lobby_id)
lobby_id = 0 lobby_id = 0
host_id = 0
for this_member in lobby_members: for this_member in lobby_members:
if this_member["steam_id"] != steam_id: if this_member["steam_id"] != steam_id:
@ -270,7 +274,13 @@ func send_p2p_packet(target: int, packet_data: Dictionary) -> void:
if this_member['steam_id'] != steam_id: if this_member['steam_id'] != steam_id:
Steam.sendP2PPacket(this_member['steam_id'], this_data, send_type, channel) Steam.sendP2PPacket(this_member['steam_id'], this_data, send_type, channel)
#print("Sent packet to %s." % this_member['steam_name']) #print("Sent packet to %s." % this_member['steam_name'])
# 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 send it to someone specific
else: else:
Steam.sendP2PPacket(target, this_data, send_type, channel) 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 "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: 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"]) 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: func register_node(node: Node) -> String:
@ -389,11 +398,23 @@ func _on_game_started():
level.add_child(player) level.add_child(player)
func sync_property(node_id: String, property_name: String, value: Variant): 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} var packet_data = {"message":"property_update", "node_id": node_id, "property_name": property_name, "value": value}
send_p2p_packet(0, packet_data) send_p2p_packet(host_id, packet_data)
func sync_property_to_peer(node_id: String, property_name: String, value: Variant, target_peer: int): 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} 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)

View File

@ -95,15 +95,15 @@ func _process(delta: float) -> void:
# If the global_position has changed, notify the NetworkManager # If the global_position has changed, notify the NetworkManager
if previous_global_position != global_position: 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 previous_global_position = global_position
if previous_global_rotation != global_rotation: 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 previous_global_rotation = global_rotation
if previous_head_vert_rotation != neck.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 previous_global_rotation = neck.rotation

View File

@ -94,12 +94,12 @@ func sync_ship_state():
if piloting_player and piloting_player.is_network_authority or NetworkManager.is_host: if piloting_player and piloting_player.is_network_authority or NetworkManager.is_host:
# Send updates only if the position has changed # Send updates only if the position has changed
if global_position != previous_position: 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 previous_position = global_position
var current_rotation = global_transform.basis.get_rotation_quaternion() var current_rotation = global_transform.basis.get_rotation_quaternion()
if current_rotation != previous_rotation: 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 previous_rotation = current_rotation
# Interpolation for non-piloting players (who only receive the updates) # Interpolation for non-piloting players (who only receive the updates)