- Edited
I'm having a lot of trouble understanding the documentation for these two pieces and am trying to implement them rather than using RPC calls since it's new in godot 4 but I'm not sure if what I'm attempting to do is the intended functionality and or correct. Not sure if I should just learn how to use the rpc functions instead and this is entirely not the purpose of the synchronizer. Just looking for some general advice on how to use these two classes together. My (probably wrong) understanding of the structure is that the synchronizer gets added and then you use the ReplicationConfig class to add the properties to that synchronizer. I'm trying to sync the position of the Sprite2D for each unit in Units. This current implementation adds a synchronizer to every child but I've also tried just creating a node on the Unit scene as well. This was just another experiment to see if that made a difference.
Just for context, I went down this path on the thought that there was a way to sync properties in a similar manner in code to the way you do it in the editor. I had synced the sprite2d positions of the units in the editor using the MultiplayerSynchronizer node by adding each property for each pre-spawned unit and this worked great but when I want to spawn new units during the game I wasn't sure how to sync those properties so I tried implementing this in my code instead. I've also been confused by the replicationconfig in general / how you connect it to the MultiplayerSynchronizer. Anyways hoping someone can clarify how these two classes work together if at all.
Here are the errors:
E 0:00:03:0834 _make_spawn_packet: Condition "sync->get_replication_config().is_null()" is true. Returning: ERR_BUG <C++ Source> modules/multiplayer/scene_replication_interface.cpp:491 @ _make_spawn_packet() E 0:00:03:0834 _send_raw: Condition "!p_buffer || p_size < 1" is true. Returning: ERR_INVALID_PARAMETER <C++ Source> modules/multiplayer/scene_replication_interface.cpp:452 @ _send_raw()
Here are the outputs:
Unit name:Unit
Sprite2D node found:/root/RTSWorld/Player_Spawn/1/Units/Unit/Sprite2D
ReplicationConfig for unitUnit:<SceneReplicationConfig#-9223371997898078860>
ReplicationConfig properties for unitUnit[":Sprite2D:position"]
MultiplayerSynchronizer for unitUnit:@@7:<MultiplayerSynchronizer#38939919731>
Here is the code:
func _ready():
# Get the current player node
var current_player_nodes = get_tree().get_nodes_in_group("current_player")
if current_player_nodes.size() > 0:
var current_player_node = current_player_nodes[0]
# Get units
get_units()
# Sync properties of Sprite2D nodes
for unit in units:
#Print
print("Unit name:", unit.get_name())
# Create a MultiplayerSynchronizer node
var multiplayer_synchronizer = MultiplayerSynchronizer.new()
# Add the MultiplayerSynchronizer node as a child of the unit node
unit.add_child(multiplayer_synchronizer)
# Create a SceneReplicationConfig
var replication_config = SceneReplicationConfig.new()
# Get the Sprite2D node
var sprite2d_node_path = NodePath("Sprite2D:position")
var sprite2d_node = unit.get_node(sprite2d_node_path)
# Check if the sprite2d_node is not null
if sprite2d_node:
print("Sprite2D node found:", sprite2d_node.get_path())
else:
print("Sprite2D node not found for unit:", unit.get_name())
# Add the position property of the Sprite2D node to the SceneReplicationConfig
var sprite2d_position_property_path = sprite2d_node_path.get_as_property_path()
add_property_if_not_exists(replication_config, sprite2d_position_property_path)
# Set the replication_config property of the MultiplayerSynchronizer
multiplayer_synchronizer.replication_config = replication_config
# Print the replication_config object
print("ReplicationConfig for unit", unit.get_name(), ":", replication_config)
# Print the properties of the SceneReplicationConfig
print("ReplicationConfig properties for unit", unit.get_name(), replication_config.get_properties())
# Set the multiplayer authority
multiplayer_synchronizer.set_multiplayer_authority(str(unit.get_name()).to_int())
# Print the state of the MultiplayerSynchronizer
print("MultiplayerSynchronizer for unit", unit.get_name(), ":", multiplayer_synchronizer)
func add_property_if_not_exists(replication_config, property_path):
if not replication_config.has_property(property_path):
replication_config.add_property(property_path)