+5 votes

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)
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
in Engine by (81 points)

2 Answers

+3 votes
Best answer

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.

by (1,397 points)
selected by

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

0 votes

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)
by (20 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.