Godot Version
4.3
Question
Hey! I’m trying to move a ball when a player collides with it. The idea I have is to call a function on the server and apply impulse there. When the player on the server collides with the ball it works perfect, but when the client collides with it and calls the function on the server this error appears:
“Invalid call. Nonexistent function ’ apply_central_impulse’ in base ‘EncodedObjectAsID’.”
This is the relevant code in player.gd:
func _physics_process(delta: float) -> void:
if is_local_authority():
if pos_to_go != Vector2.ZERO:
var dir = global_position.direction_to(pos_to_go)
velocity = speed * dir.normalized()
if global_position.distance_squared_to(pos_to_go) > 4:
rotation = dir.angle()
var collision = move_and_collide(velocity * delta)
if collision != null:
var collider = collision.get_collider()
if collider is RigidBody2D: # it's the ball
var ball = collider as RigidBody2D
var push_direction = -collision.get_normal()
if multiplayer.is_server():
print("Called from server...")
tapBall(ball, push_direction * push_force)
else:
print("Called from client...")
tapBall.rpc(ball, push_direction * push_force)
else:
pos_to_go = Vector2.ZERO
processInput(delta)
@rpc ("any_peer")
func tapBall(ball, force):
ball.apply_central_impulse(force)
EDIT: I changed Ball to be a Singleton, now it works BUT another issue arised which is that the Ball doesn’t show on the map (I know it works because I made the camera follow the ball). I asume it’s beign draw before the floor (it’s top down view). Is there a way to change the drawing order?