From b9cf3ff9e1bc4dd8f8d0a882bcbcf33187cb5152 Mon Sep 17 00:00:00 2001 From: Chris Bell Date: Sat, 14 Dec 2024 21:10:48 -0600 Subject: [PATCH] tarting work on syncronizer node --- .../core/networking/scripts/NetworkManager.gd | 7 ++++++ assets/core/networking/scripts/Syncronizer.gd | 25 +++++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 assets/core/networking/scripts/Syncronizer.gd diff --git a/assets/core/networking/scripts/NetworkManager.gd b/assets/core/networking/scripts/NetworkManager.gd index 2e2d701..5859d7a 100644 --- a/assets/core/networking/scripts/NetworkManager.gd +++ b/assets/core/networking/scripts/NetworkManager.gd @@ -25,6 +25,8 @@ signal user_left_lobby(user_id: int) signal host_left_lobby() signal on_game_started() +signal property_update_received(node_id: int, property_name: String, value: Variant) + func _ready() -> void: player_scene = load("res://assets/core/player-controller/scenes/player.tscn") as PackedScene @@ -300,6 +302,11 @@ func read_p2p_packet() -> void: if "message" in readable_data and readable_data["message"] == "start_game": print("Received game start packet.") _on_game_started() + + # Property update packet + 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: + property_update_received.emit(readable_data["node_id"], readable_data["property_name"], readable_data["value"]) # Print the packet to output diff --git a/assets/core/networking/scripts/Syncronizer.gd b/assets/core/networking/scripts/Syncronizer.gd new file mode 100644 index 0000000..eb7f8e8 --- /dev/null +++ b/assets/core/networking/scripts/Syncronizer.gd @@ -0,0 +1,25 @@ +extends Node + +class_name Syncronizer + +@export var properties_to_sync: Array = [] + +signal property_changed(node_id: int, property_name: String, value: Variant) + +func _ready() -> void: + for property_name in properties_to_sync: + self.connect("property_changed", Callable(self, "_on_property_changed")) + + NetworkManager.property_update_received.connect(Callable(self, "_on_property_update_received")) + + +func _on_property_changed(node_id: int, property_name: String, value: Variant) -> void: + if node_id == get_instance_id() and property_name in properties_to_sync: + self.set(property_name, value) + emit_signal("property_changed", node_id, property_name, value) + + +func set_property(property_name: String, value: Variant) -> void: + if property_name in properties_to_sync: + self.set(property_name, value) + emit_signal("property_changed", get_instance_id(), property_name, value) \ No newline at end of file