Godot Version
4.4.1
Question
The host doesn’t sync with the clients
The clients also receive this error in the debugger
The player scene contains a MultiplayerSynchronizer called sync, the player node name is their unique id, and the island in instantiated inside the Main node
I also have a GameManager.players, where i have every player in game
and when a new client connects, all the clients that are already connected (excluding the host) their GameManager.players is not updated, it remains the the players that we’re connected when it joined
For example, I have 4 players, player 1 hosts, player 2 joins, they both have 2 players in the GameManager.players, now player 3 joins, host has 3 players, but player 2 still has 2 players. Then player 4 joins, host has 4 players, but player 2 still has 2 and player 3 still has 3.
This is the code:
extends Node
var godotPeer: ENetMultiplayerPeer
var address: String = "127.0.0.1"
var port: int = 8910
var lobbyNetwork: int = 0 # 0 = Steam, 1 = Godot
const PLAYER_ISLAND = preload("res://Resources/World/PlayerIsland.tscn")
const PLAYER = preload("res://Resources/Entities/Humanoid/Player.tscn")
var island: Node2D
func _init() -> void:
OS.set_environment("SteamAppID", appID)
OS.set_environment("SteamGameID", appID)
func _ready() -> void:
Steam.steamInitEx(int(appID))
var isRunning: bool = Steam.isSteamRunning()
var id: int
var username: String
if isRunning:
id = Steam.getSteamID()
username = Steam.getFriendPersonaName(id)
print_rich("[color=green][b]Steam is running![color=yellow] User: " + username + "[/color][/b]")
else:
print_rich("[color=red][b]Steam not is running![/color][/b]")
#------- Godot
multiplayer.peer_connected.connect(peer_connected)
multiplayer.peer_disconnected.connect(peer_disconnected)
multiplayer.connected_to_server.connect(connected_to_server)
multiplayer.connection_failed.connect(connection_failed)
func _process(delta: float) -> void:
Steam.run_callbacks()
func startGame() -> void:
island = PLAYER_ISLAND.instantiate()
var children: Array = get_parent().get_children()
for x in children:
if x.name == "Main" and x is Node2D:
x.add_child(island)
func hostGodotLobby() -> void:
Network.godotPeer = ENetMultiplayerPeer.new()
var peer: ENetMultiplayerPeer = Network.godotPeer
var error = peer.create_server(Network.port, 4)
if not error == OK:
push_error("cannot host: " + str(error))
return
peer.get_host().compress(ENetConnection.COMPRESS_RANGE_CODER)
multiplayer.set_multiplayer_peer(peer)
startGame()
var host_id = multiplayer.get_unique_id()
GameManager.players[host_id] = PlayerData.new()
spawnPlayer.rpc(host_id)
print("Game Hosted!")
func joinGodotLobby() -> void:
Network.godotPeer = ENetMultiplayerPeer.new()
var peer: ENetMultiplayerPeer = Network.godotPeer
peer.create_client(Network.address, Network.port)
peer.get_host().compress(ENetConnection.COMPRESS_RANGE_CODER)
multiplayer.set_multiplayer_peer(peer)
print("Game Joined!")
@rpc("any_peer", "call_local")
func spawnPlayer(id: int) -> void:
if is_instance_valid(island) and island.has_node(str(id)):
print_rich("[color=yellow]Player " + str(id) + " already exists[/color]")
return
var player: CharacterBody2D = PLAYER.instantiate()
var playerSynchronizer: MultiplayerSynchronizer = player.find_child("Sync")
if playerSynchronizer:
playerSynchronizer.set_process_mode(Node.PROCESS_MODE_DISABLED)
player.find_child("ID").text = str(id)
player.set_multiplayer_authority(id)
if is_instance_valid(island):
player.name = str(id)
island.add_child(player)
else: printerr("Island doesn't exist")
await get_tree().process_frame
playerSynchronizer.set_process_mode(Node.PROCESS_MODE_INHERIT)
@rpc("authority", "call_local", "reliable")
func sendPlayerList(list: Dictionary) -> void:
if multiplayer.is_server():
return
var playerList: Dictionary[int, PlayerData] = {}
for x in list:
var serializedPlayer = list[x]
playerList[x] = PlayerData.new().convertToPlayerData(serializedPlayer)
GameManager.players = playerList
@rpc("any_peer")
func requestInitialState() -> void:
if not multiplayer.is_server(): return
var senderID = multiplayer.get_remote_sender_id()
var serializedList = {}
for id in GameManager.players:
serializedList[id] = GameManager.players[id].convertToDict()
sendPlayerList.rpc_id(senderID, serializedList)
for x in GameManager.players:
spawnPlayer.rpc_id(senderID, x)
func peer_connected(id: int):
if not multiplayer.is_server():
return
if not GameManager.players.has(id):
GameManager.players[id] = PlayerData.new()
spawnPlayer.rpc(id)
func peer_disconnected(id: int):
if multiplayer.is_server():
if GameManager.players.has(id):
GameManager.players.erase(id)
func connected_to_server():
startGame()
await get_tree().process_frame
requestInitialState.rpc_id(1)
func connection_failed():
pass
Please help I’m going insane