|
|
|
@@ -73,6 +73,7 @@ func _on_lobby_created(connect: int, this_lobby_id: int):
|
|
|
|
# Spawn self
|
|
|
|
# Spawn self
|
|
|
|
spawn_player.rpc(1)
|
|
|
|
spawn_player.rpc(1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# -----------------------------------------------------------------------------
|
|
|
|
# -----------------------------------------------------------------------------
|
|
|
|
# CORE LOGIC: STEP 2 - CLIENT CONNECTION
|
|
|
|
# CORE LOGIC: STEP 2 - CLIENT CONNECTION
|
|
|
|
# -----------------------------------------------------------------------------
|
|
|
|
# -----------------------------------------------------------------------------
|
|
|
|
@@ -88,67 +89,75 @@ func _on_lobby_joined(this_lobby_id: int, _p, _l, response: int):
|
|
|
|
lobby_id = this_lobby_id
|
|
|
|
lobby_id = this_lobby_id
|
|
|
|
print("[CLIENT] 2. Successfully joined Steam lobby.")
|
|
|
|
print("[CLIENT] 2. Successfully joined Steam lobby.")
|
|
|
|
|
|
|
|
|
|
|
|
# Only clients should attempt to connect via peer. The host is already a peer.
|
|
|
|
|
|
|
|
if Steam.getLobbyOwner(lobby_id) != steam_id:
|
|
|
|
if Steam.getLobbyOwner(lobby_id) != steam_id:
|
|
|
|
print("[CLIENT] 3. I am a client, creating multiplayer peer to connect to host.")
|
|
|
|
print("[CLIENT] 3. I am a client, creating multiplayer peer to connect to host.")
|
|
|
|
setup_multiplayer_peer(false)
|
|
|
|
setup_multiplayer_peer(false)
|
|
|
|
|
|
|
|
|
|
|
|
# This signal fires on the CLIENT after setup_multiplayer_peer succeeds
|
|
|
|
|
|
|
|
func _on_connected_to_server():
|
|
|
|
func _on_connected_to_server():
|
|
|
|
print("[CLIENT] 4. Successfully connected to host's multiplayer peer.")
|
|
|
|
print("[CLIENT] 4. Successfully connected to host's multiplayer peer.")
|
|
|
|
print("[CLIENT] 5. Sending my info to the server for registration...")
|
|
|
|
print("[CLIENT] 5. Sending my info to the server for registration...")
|
|
|
|
register_player_on_server.rpc_id(1, steam_id)
|
|
|
|
register_player_on_server.rpc_id(1, steam_id)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# -----------------------------------------------------------------------------
|
|
|
|
# -----------------------------------------------------------------------------
|
|
|
|
# CORE LOGIC: STEP 3 - SERVER REGISTRATION & SPAWNING
|
|
|
|
# CORE LOGIC: STEP 3 - SERVER REGISTRATION & SPAWNING
|
|
|
|
# -----------------------------------------------------------------------------
|
|
|
|
# -----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
@rpc("any_peer")
|
|
|
|
# This RPC is sent by a client and runs ONLY on the SERVER
|
|
|
|
|
|
|
|
@rpc("authority", "call_local")
|
|
|
|
|
|
|
|
func register_player_on_server(new_player_steam_id: int):
|
|
|
|
func register_player_on_server(new_player_steam_id: int):
|
|
|
|
|
|
|
|
if not multiplayer.is_server(): return
|
|
|
|
|
|
|
|
|
|
|
|
var new_player_peer_id = multiplayer.get_remote_sender_id()
|
|
|
|
var new_player_peer_id = multiplayer.get_remote_sender_id()
|
|
|
|
print("[SERVER] 6. Received registration request from Peer %s." % new_player_peer_id)
|
|
|
|
print("[SERVER] 6. Received registration request from Peer %s." % new_player_peer_id)
|
|
|
|
|
|
|
|
|
|
|
|
# Add the new player to the map
|
|
|
|
|
|
|
|
peer_to_steam_id_map[new_player_peer_id] = new_player_steam_id
|
|
|
|
peer_to_steam_id_map[new_player_peer_id] = new_player_steam_id
|
|
|
|
print("[SERVER] 7. Peer %s (Steam ID: %s) added to ID map." % [new_player_peer_id, new_player_steam_id])
|
|
|
|
print("[SERVER] 7. Peer %s (Steam ID: %s) added to ID map." % [new_player_peer_id, new_player_steam_id])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# --- NEW STEP ---
|
|
|
|
|
|
|
|
# Sync the completed map to the new client BEFORE spawning players for them
|
|
|
|
|
|
|
|
print("[SERVER] 8. Sending complete ID map to new client (Peer %s)." % new_player_peer_id)
|
|
|
|
|
|
|
|
sync_id_map.rpc_id(new_player_peer_id, peer_to_steam_id_map)
|
|
|
|
|
|
|
|
|
|
|
|
# Tell the new client about all players already in the game
|
|
|
|
# Tell the new client about all players already in the game
|
|
|
|
print("[SERVER] 8. Telling Peer %s about existing players..." % new_player_peer_id)
|
|
|
|
print("[SERVER] 9. Telling Peer %s to spawn existing players..." % new_player_peer_id)
|
|
|
|
for existing_peer_id in players:
|
|
|
|
for existing_peer_id in players:
|
|
|
|
spawn_player.rpc_id(new_player_peer_id, existing_peer_id)
|
|
|
|
spawn_player.rpc_id(new_player_peer_id, existing_peer_id)
|
|
|
|
|
|
|
|
|
|
|
|
# Tell EVERYONE to spawn the new player
|
|
|
|
# Tell EVERYONE to spawn the new player
|
|
|
|
print("[SERVER] 9. Telling ALL peers to spawn the new player (Peer %s)." % new_player_peer_id)
|
|
|
|
print("[SERVER] 10. Telling ALL peers to spawn the new player (Peer %s)." % new_player_peer_id)
|
|
|
|
spawn_player.rpc(new_player_peer_id)
|
|
|
|
spawn_player.rpc(new_player_peer_id)
|
|
|
|
|
|
|
|
|
|
|
|
# This RPC is sent by the SERVER and runs on ALL clients
|
|
|
|
# --- NEW FUNCTION ---
|
|
|
|
|
|
|
|
# This RPC is sent by the server to a new client to give them the ID map
|
|
|
|
|
|
|
|
@rpc("authority")
|
|
|
|
|
|
|
|
func sync_id_map(map_from_server: Dictionary):
|
|
|
|
|
|
|
|
print("-> [%s] Received ID map from server." % multiplayer.get_unique_id())
|
|
|
|
|
|
|
|
peer_to_steam_id_map = map_from_server
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@rpc("any_peer", "call_local")
|
|
|
|
@rpc("any_peer", "call_local")
|
|
|
|
func spawn_player(peer_id: int):
|
|
|
|
func spawn_player(peer_id: int):
|
|
|
|
var steam_id_of_player = peer_to_steam_id_map.get(peer_id)
|
|
|
|
var steam_id_of_player = peer_to_steam_id_map.get(peer_id)
|
|
|
|
if steam_id_of_player == null:
|
|
|
|
if steam_id_of_player == null:
|
|
|
|
print("!!! [%s] Cannot spawn player for Peer %s, not yet in map." % [multiplayer.get_unique_id(), peer_id])
|
|
|
|
print("!!! [%s] CRITICAL: Cannot spawn player for Peer %s, not in map." % [multiplayer.get_unique_id(), peer_id])
|
|
|
|
return
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
|
if players.has(peer_id): # Prevents spawning a duplicate
|
|
|
|
if players.has(peer_id): return
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var player_name = Steam.getFriendPersonaName(steam_id_of_player)
|
|
|
|
var player_name = Steam.getFriendPersonaName(steam_id_of_player)
|
|
|
|
print("-> [%s] Spawning character for Peer %s (Name: %s)." % [multiplayer.get_unique_id(), peer_id, player_name])
|
|
|
|
print("-> [%s] Spawning character for Peer %s (Name: %s)." % [multiplayer.get_unique_id(), peer_id, player_name])
|
|
|
|
|
|
|
|
|
|
|
|
var new_player = player_scene.instantiate()
|
|
|
|
var new_player = player_scene.instantiate() as Player
|
|
|
|
new_player.name = str(peer_id)
|
|
|
|
new_player.name = str(peer_id)
|
|
|
|
|
|
|
|
|
|
|
|
players[peer_id] = new_player
|
|
|
|
players[peer_id] = new_player
|
|
|
|
add_child(new_player)
|
|
|
|
add_child(new_player)
|
|
|
|
|
|
|
|
new_player.position = Vector3.UP
|
|
|
|
new_player.set_player_name(player_name)
|
|
|
|
new_player.set_player_name(player_name)
|
|
|
|
|
|
|
|
|
|
|
|
if multiplayer.is_server():
|
|
|
|
|
|
|
|
new_player.set_multiplayer_authority(peer_id)
|
|
|
|
new_player.set_multiplayer_authority(peer_id)
|
|
|
|
|
|
|
|
|
|
|
|
# -----------------------------------------------------------------------------
|
|
|
|
# -----------------------------------------------------------------------------
|
|
|
|
# UTILITY AND CALLBACKS
|
|
|
|
# UTILITY AND CALLBACKS (Unchanged)
|
|
|
|
# -----------------------------------------------------------------------------
|
|
|
|
# -----------------------------------------------------------------------------
|
|
|
|
func _on_p2p_session_request(steam_id_remote: int) -> void:
|
|
|
|
func _on_p2p_session_request(steam_id_remote: int) -> void:
|
|
|
|
print("[P2P] ==> Session request from: %s. Accepting." % steam_id_remote)
|
|
|
|
print("[P2P] ==> Session request from: %s. Accepting." % steam_id_remote)
|
|
|
|
@@ -170,10 +179,10 @@ func setup_multiplayer_peer(is_host: bool = false) -> void:
|
|
|
|
|
|
|
|
|
|
|
|
peer = SteamMultiplayerPeer.new()
|
|
|
|
peer = SteamMultiplayerPeer.new()
|
|
|
|
if is_host:
|
|
|
|
if is_host:
|
|
|
|
peer.create_host()
|
|
|
|
peer.create_host(0)
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
var host_id = Steam.getLobbyOwner(lobby_id)
|
|
|
|
var host_id = Steam.getLobbyOwner(lobby_id)
|
|
|
|
peer.create_client(host_id)
|
|
|
|
peer.create_client(host_id, 0)
|
|
|
|
|
|
|
|
|
|
|
|
multiplayer.multiplayer_peer = peer
|
|
|
|
multiplayer.multiplayer_peer = peer
|
|
|
|
|
|
|
|
|
|
|
|
@@ -191,7 +200,7 @@ func _on_peer_disconnected(id: int):
|
|
|
|
func _on_connection_failed() -> void:
|
|
|
|
func _on_connection_failed() -> void:
|
|
|
|
print("[CLIENT] !!! Connection to the host failed.")
|
|
|
|
print("[CLIENT] !!! Connection to the host failed.")
|
|
|
|
|
|
|
|
|
|
|
|
func _on_lobby_chat_update(lobby_id_update: int, user_changed_id: int, user_making_change_id: int, chat_state: int):
|
|
|
|
func _on_lobby_chat_update(_l_id, user_changed_id: int, _u_m_c_id, chat_state: int):
|
|
|
|
var state_string = "UNKNOWN"
|
|
|
|
var state_string = "UNKNOWN"
|
|
|
|
match chat_state:
|
|
|
|
match chat_state:
|
|
|
|
Steam.CHAT_MEMBER_STATE_CHANGE_ENTERED: state_string = "ENTERED"
|
|
|
|
Steam.CHAT_MEMBER_STATE_CHANGE_ENTERED: state_string = "ENTERED"
|
|
|
|
|