I have been making a 2 player multiplayer game in Godot. When I tried to sync animations with the host and the client, Godot was throwing an error: Function Play() is not available in the AnimationPlayer class. when I remove the code that syncs the animations together in the host and client it would work like nothing was wrong.
I have been trouble shooting for a couple hours but I coudn't find any fixes!
Player.gd:
extends KinematicBody2D
const ACCELERAION = 512
const MAX_SPEED = 64
const FRICTION = 0.25
const GRAVIY = 200
const JUMP_FORCE = 128
var motion = Vector2.ZERO
onready var sprite = $Sprite
onready var animationPlayer = $AnimationPlayer
remote func _set_positon_and_animation(pos, hFlip, animPlayer):
global_transform.origin = pos
sprite.flip_h = hFlip
animationPlayer = animPlayer
func _physics_process(delta):
var xInput = Input.get_action_strength("ui_right") - Input.get_action_strength("ui_left")
print(xInput)
if xInput != 0:
animationPlayer.play("Running")
motion.x += xInput * ACCELERAION * delta
motion.x = clamp(motion.x, -MAX_SPEED, MAX_SPEED)
if is_network_master():
sprite.flip_h = xInput < 0
else:
animationPlayer.play("Standing")
motion.y += GRAVIY * delta
if is_on_floor():
if xInput == 0:
motion.x = lerp(motion.x, 0, FRICTION)
if Input.is_action_just_pressed("ui_up"):
motion.y = -JUMP_FORCE
else:
animationPlayer.play("Jumping")
if Input.is_action_just_released("ui_up") and motion.y < -JUMP_FORCE / 2:
motion.y = -JUMP_FORCE / 2
if xInput == 0:
motion.x = lerp(motion.x, 0, 0.2)
if motion != Vector2():
if is_network_master():
motion = move_and_slide(motion, Vector2.UP)
rpc_unreliable("_set_positon_and_animation", global_transform.origin, sprite.flip_h, animationPlayer)
#rpc_unreliable("_set_animation", animationPlayer)
Specific code that syncs the client and the host in player.gd:
remote func _set_positon_and_animation(pos, hFlip, animPlayer):
global_transform.origin = pos
sprite.flip_h = hFlip
animationPlayer = animPlayer
if is_network_master():
sprite.flip_h = xInput < 0
if motion != Vector2():
if is_network_master():
motion = move_and_slide(motion, Vector2.UP)
rpc_unreliable("_set_positon_and_animation", global_transform.origin, sprite.flip_h, animationPlayer)
#rpc_unreliable("_set_animation", animationPlayer)
World.gd:
extends Node2D
onready var playerSpawn = $SpawnPoint
onready var playerSpawn2 = $SpawnPoint2
func _ready():
var player1 = preload("res://Player.tscn").instance()
player1.set_name(str(get_tree().get_network_unique_id())) #Gives the host player a unique id when they join the server
player1.set_network_master(get_tree().get_network_unique_id()) # Specifies this player owns a different character controller than the other player
player1.set_global_transform(playerSpawn.global_transform)
add_child(player1)
var player2 = preload("res://Player.tscn").instance()
player2.set_name(str(Globals.player2id)) #Gets the id the player got when they joined the game cant use the same function as player 1 because it will give us the same id and crash the game
player2.set_network_master(Globals.player2id) # Specifies this player owns a different character controller than the other player
player2.set_global_transform(playerSpawn2.global_transform)
add_child(player2)