What is the correct way to sync rigid bodies between network clients and server?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By wowzzers

I have a simple rigid body cube and I want to sync its position and rotation between clients and server in the network. What is the correct way?

I’ve attached this script to all my boxes (rigid bodies):

extends RigidBody
func _physics_process(delta):
   rpc_unreliable("update_pos_rot", translation, rotation)
   pass
puppet func update_pos_rot(pos, rot):
   translation = pos
   rotation = rot

It kind of works but I have this error in the console:

ERROR: _process_rpc: Condition ` !_can_call_mode(p_node, rpc_mode, p_from) ` is true.
At: core/io/multiplayer_api.cpp:286
:bust_in_silhouette: Reply From: omggomb

I don’t know if there is a correct way, but considering that you shouldn’t directly set position and rotation of rigidbodies (See here), I’d only simulate physics on the server and on clients use no rigidbodies at all and instead only sync the position of meshinstances, or kinematicbodies on client with rigidbodies on the server.

Thank you for the answer! I’ll try to sync mesh instances instead.

wowzzers | 2019-03-29 22:03

:bust_in_silhouette: Reply From: Steely Wing

I am also finding the correct way to sync physics, but the error is cause by your function call from wrong peer

ERROR: _process_rpc: Condition ` !_can_call_mode(p_node, rpc_mode, p_from) ` is true.

Look like your server receive the RPC from the client, and server (master) will not allow client (puppet) to call (because you have set puppet on the func), try to add a check so client will not call the RPC

func _physics_process(delta):
    if get_tree().is_network_server():
        rpc_unreliable("update_pos_rot", translation, rotation)