From 80211ba8275ffe5a585c3fc6d948394b0f60d2eb Mon Sep 17 00:00:00 2001 From: MrGadget1024 <9826063+MrGadget1024@users.noreply.github.com> Date: Thu, 9 Feb 2023 11:21:06 -0500 Subject: [PATCH] fix: NetworkRoomManager.ReadyStatusChange is now a virtual method - Script Template updated accordingly. Fixes #1889 --- .../Mirror/Components/NetworkRoomManager.cs | 125 +++++++++--------- ... Room Manager-NewNetworkRoomManager.cs.txt | 8 ++ 2 files changed, 72 insertions(+), 61 deletions(-) diff --git a/Assets/Mirror/Components/NetworkRoomManager.cs b/Assets/Mirror/Components/NetworkRoomManager.cs index 873689a6b..e210e730f 100644 --- a/Assets/Mirror/Components/NetworkRoomManager.cs +++ b/Assets/Mirror/Components/NetworkRoomManager.cs @@ -118,47 +118,6 @@ public override void OnValidate() } } - public void ReadyStatusChanged() - { - int CurrentPlayers = 0; - int ReadyPlayers = 0; - - foreach (NetworkRoomPlayer item in roomSlots) - { - if (item != null) - { - CurrentPlayers++; - if (item.readyToBegin) - ReadyPlayers++; - } - } - - if (CurrentPlayers == ReadyPlayers) - CheckReadyToBegin(); - else - allPlayersReady = false; - } - - /// - /// Called on the server when a client is ready. - /// The default implementation of this function calls NetworkServer.SetClientReady() to continue the network setup process. - /// - /// Connection from client. - public override void OnServerReady(NetworkConnectionToClient conn) - { - Debug.Log($"NetworkRoomManager OnServerReady {conn}"); - base.OnServerReady(conn); - - if (conn != null && conn.identity != null) - { - GameObject roomPlayer = conn.identity.gameObject; - - // if null or not a room player, don't replace it - if (roomPlayer != null && roomPlayer.GetComponent() != null) - SceneLoadedForPlayer(conn, roomPlayer); - } - } - void SceneLoadedForPlayer(NetworkConnectionToClient conn, GameObject roomPlayer) { Debug.Log($"NetworkRoom SceneLoadedForPlayer scene: {SceneManager.GetActiveScene().path} {conn}"); @@ -190,6 +149,26 @@ void SceneLoadedForPlayer(NetworkConnectionToClient conn, GameObject roomPlayer) NetworkServer.ReplacePlayerForConnection(conn, gamePlayer, true); } + internal void CallOnClientEnterRoom() + { + OnRoomClientEnter(); + foreach (NetworkRoomPlayer player in roomSlots) + if (player != null) + { + player.OnClientEnterRoom(); + } + } + + internal void CallOnClientExitRoom() + { + OnRoomClientExit(); + foreach (NetworkRoomPlayer player in roomSlots) + if (player != null) + { + player.OnClientExitRoom(); + } + } + /// /// CheckReadyToBegin checks all of the players in the room to see if their readyToBegin flag is set. /// If all of the players are ready, then the server switches from the RoomScene to the PlayScene, essentially starting the game. This is called automatically in response to NetworkRoomPlayer.CmdChangeReadyState. @@ -215,26 +194,6 @@ public void CheckReadyToBegin() allPlayersReady = false; } - internal void CallOnClientEnterRoom() - { - OnRoomClientEnter(); - foreach (NetworkRoomPlayer player in roomSlots) - if (player != null) - { - player.OnClientEnterRoom(); - } - } - - internal void CallOnClientExitRoom() - { - OnRoomClientExit(); - foreach (NetworkRoomPlayer player in roomSlots) - if (player != null) - { - player.OnClientExitRoom(); - } - } - #region server handlers /// @@ -301,6 +260,26 @@ public override void OnServerDisconnect(NetworkConnectionToClient conn) // Sequential index used in round-robin deployment of players into instances and score positioning public int clientIndex; + /// + /// Called on the server when a client is ready. + /// The default implementation of this function calls NetworkServer.SetClientReady() to continue the network setup process. + /// + /// Connection from client. + public override void OnServerReady(NetworkConnectionToClient conn) + { + Debug.Log($"NetworkRoomManager OnServerReady {conn}"); + base.OnServerReady(conn); + + if (conn != null && conn.identity != null) + { + GameObject roomPlayer = conn.identity.gameObject; + + // if null or not a room player, don't replace it + if (roomPlayer != null && roomPlayer.GetComponent() != null) + SceneLoadedForPlayer(conn, roomPlayer); + } + } + /// /// Called on the server when a client adds a new player with NetworkClient.AddPlayer. /// The default implementation for this function creates a new player object from the playerPrefab. @@ -597,6 +576,30 @@ public virtual bool OnRoomServerSceneLoadedForPlayer(NetworkConnectionToClient c return true; } + /// + /// This is called on server from NetworkRoomPlayer.CmdChangeReadyState when client indicates change in Ready status. + /// + public virtual void ReadyStatusChanged() + { + int CurrentPlayers = 0; + int ReadyPlayers = 0; + + foreach (NetworkRoomPlayer item in roomSlots) + { + if (item != null) + { + CurrentPlayers++; + if (item.readyToBegin) + ReadyPlayers++; + } + } + + if (CurrentPlayers == ReadyPlayers) + CheckReadyToBegin(); + else + allPlayersReady = false; + } + /// /// This is called on the server when all the players in the room are ready. /// The default implementation of this function uses ServerChangeScene() to switch to the game player scene. By implementing this callback you can customize what happens when all the players in the room are ready, such as adding a countdown or a confirmation for a group leader. diff --git a/Assets/ScriptTemplates/54-Mirror__Network Room Manager-NewNetworkRoomManager.cs.txt b/Assets/ScriptTemplates/54-Mirror__Network Room Manager-NewNetworkRoomManager.cs.txt index 4c0b5111f..608599697 100644 --- a/Assets/ScriptTemplates/54-Mirror__Network Room Manager-NewNetworkRoomManager.cs.txt +++ b/Assets/ScriptTemplates/54-Mirror__Network Room Manager-NewNetworkRoomManager.cs.txt @@ -105,6 +105,14 @@ public class #SCRIPTNAME# : NetworkRoomManager return base.OnRoomServerSceneLoadedForPlayer(conn, roomPlayer, gamePlayer); } + /// + /// This is called on server from NetworkRoomPlayer.CmdChangeReadyState when client indicates change in Ready status. + /// + public override void ReadyStatusChanged() + { + base.ReadyStatusChanged(); + } + /// /// This is called on the server when all the players in the room are ready. /// The default implementation of this function uses ServerChangeScene() to switch to the game player scene. By implementing this callback you can customize what happens when all the players in the room are ready, such as adding a countdown or a confirmation for a group leader.