Why the players position is not correct?

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

It’s my first time trying to create a multiplayer game, and it’s being really hard for me.
The game works fine if I use just one client, but if another client connects, the position of the characters get really strange.

Server script:

extends Node2D


const port = 6666
var maxPlayers = 10


func _ready():
	var network = NetworkedMultiplayerENet.new()
	var res = network.create_server(port, maxPlayers)
	if res != OK:
		print("Error during the creation of the server.")
		return
	get_tree().set_network_peer(network)

	var _connected = network.connect("peer_connected", self, "_peer_connected")
	var _disconnected = network.connect("peer_disconnected", self, "_peer_disconnected")


func _peer_connected(id):
	Globals.playerIDs.append(id)
	print(str(id) + " connected.")
	print("Users connected: " + str(Globals.playerIDs))


func _peer_disconnected(id):
	print(str(id) + " disconnected.")
	Globals.playerIDs.erase(id)
	print("Users connected: " + str(Globals.playerIDs))

Client script:

extends Node2D


const IP = "127.0.0.1"
const port = 6666
var _failedConnection
var _connection


func _on_connection_failed(error):
	print("Error while trying to connect to the server:" + str(error))


remote func registerID():
	var ID = get_tree().get_rpc_sender_id()
	Globals.playerIDs.append(ID)


func _on_connection(ID):
	rpc_id(ID,"registerID")
	var game = preload("res://scenes/Game.tscn").instance()
	get_tree().get_root().add_child(game)
	hide()


func _on_Disconnect_pressed():
	print(str(Globals.playerIDs))
	get_tree().set_network_peer(null)


func _on_Connect_pressed():
	var network = NetworkedMultiplayerENet.new()
	network.create_client(IP, port)
	get_tree().set_network_peer(network)
# warning-ignore:return_value_discarded
	get_tree().multiplayer.connect("network_peer_connected", self, "_on_connection")

Game script:

extends Node2D


func _ready():
	for ID in Globals.playerIDs:
		var player = preload("res://scenes/Player.tscn").instance()
		player.set_name(ID)
		player.set_network_master(ID)
		add_child(player)
	var SELF = preload("res://scenes/Player.tscn").instance()
	SELF.set_name(str(get_tree().get_network_unique_id()))
	SELF.set_network_master(get_tree().get_network_unique_id())
	add_child(SELF)

Player script:

extends KinematicBody2D
var direction = Vector2(0,0)
export var speed = 1
var movement
signal moving


puppet func setPosition(pos):
	position = pos

func _ready():
	speed *= 50000
	#loadPlayer()

func _process(delta):
	direction = Vector2(0,0)
	if is_network_master():
		direction.x += float(Input.is_action_pressed("ui_right"))
		direction.x -= float(Input.is_action_pressed("ui_left"))
		direction.y -= float(Input.is_action_pressed("ui_up"))
		direction.y += float(Input.is_action_pressed("ui_down"))

		if direction != Vector2(0, 0):
			emit_signal("moving")
		if direction.length() != 0:
			direction = direction.normalized()
		rpc_unreliable("setPosition", position)
	move_and_slide(direction * speed * delta)

How can I fix that?

:bust_in_silhouette: Reply From: Leirda

What do you mean by “the position gets really strange”?
Could you be more accurate in the description of the unwanted behavior?

At a first glance, I would try to switch the move_and_slide() and the rpc_unreliable() calls, setting position before sending it, otherwise you send some wrong position to the server:

# Player script
func _process(delta):
    direction = Vector2(0,0)
    move_and_slide(direction * speed * delta) # move here...

    if is_network_master():
        [...]
        rpc_unreliable("setPosition", position)
    # ... instead of here!