Skip to content

Commit 31ee332

Browse files
committed
workaround for fixing the loading screen never going away on clients if the main menu scene switches too fast.
1 parent fc1ed64 commit 31ee332

File tree

3 files changed

+22
-10
lines changed

3 files changed

+22
-10
lines changed

Assets/BossRoom/Scripts/Shared/Net/ConnectionManagement/ClientGameNetPortal.cs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -142,17 +142,14 @@ public void OnConnectFinished(ConnectStatus status)
142142
}
143143
}
144144

145-
private void OnDisconnectReasonReceived(ConnectStatus status)
146-
{
147-
DisconnectReason.SetDisconnectReason(status);
148-
}
149-
150-
private void OnDisconnectOrTimeout(ulong clientID)
145+
void OnDisconnectOrTimeout(ulong clientID)
151146
{
152147
// This is also called on the Host when a different client disconnects. To make sure we only handle our own disconnection, verify that we are either
153148
// not a host (in which case we know this is about us) or that the clientID is the same as ours if we are the host.
154149
if (!NetworkManager.Singleton.IsHost || NetworkManager.Singleton.IsHost && NetworkManager.Singleton.LocalClientId == clientID)
155150
{
151+
SceneLoaderWrapper.Instance.IsClosingClients = false; // disconnect done
152+
156153
//On a client disconnect we want to take them back to the main menu.
157154
//We have to check here in SceneManager if our active scene is the main menu, as if it is, it means we timed out rather than a raw disconnect;
158155
if (SceneManager.GetActiveScene().name != "MainMenu")
@@ -307,7 +304,8 @@ public static void ReceiveServerToClientConnectResult_CustomMessage(ulong client
307304
public static void ReceiveServerToClientSetDisconnectReason_CustomMessage(ulong clientID, FastBufferReader reader)
308305
{
309306
reader.ReadValueSafe(out ConnectStatus status);
310-
Instance.OnDisconnectReasonReceived(status);
307+
Instance.DisconnectReason.SetDisconnectReason(status);
308+
SceneLoaderWrapper.Instance.IsClosingClients = true;
311309
}
312310
}
313311
}

Assets/BossRoom/Scripts/Shared/Net/ConnectionManagement/ServerGameNetPortal.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,15 +110,20 @@ public void OnUserDisconnectRequest()
110110
{
111111
SendServerToAllClientsSetDisconnectReason(ConnectStatus.HostDisconnected);
112112
// wait before shutting down to make sure those messages get sent before the clients disconnect.
113-
StartCoroutine(WaitToShutdown());
113+
StartCoroutine(WaitToShutdown()); // todo in theory, according to "Shutdown"'s doc, we shouldn't need this wait anymore.
114114
}
115115
}
116116

117117
IEnumerator WaitToShutdown()
118118
{
119-
yield return new WaitForSeconds(0.5f);
119+
// todo netmanager should really be shutting down instead of us having to manually flag that we're shutting down
120+
SceneLoaderWrapper.Instance.IsClosingClients = true;
121+
yield return null; // todo still needed? wait for UTP's update for it to send it's batched messages
122+
yield return null;
120123
SessionManager<SessionPlayerData>.Instance.OnUserDisconnectRequest();
124+
Debug.Log("shutdown");
121125
m_Portal.NetManager.Shutdown();
126+
SceneLoaderWrapper.Instance.IsClosingClients = false;
122127
}
123128

124129
private void Clear()
@@ -243,6 +248,7 @@ public void SendServerToClientSetDisconnectReason(ulong clientID, ConnectStatus
243248
writer.WriteValueSafe(status);
244249
NetworkManager.Singleton.CustomMessagingManager.SendNamedMessage(nameof(ClientGameNetPortal.ReceiveServerToClientSetDisconnectReason_CustomMessage), clientID, writer);
245250
}
251+
246252
/// <summary>
247253
/// Sends a DisconnectReason to all connected clients. This should only be done on the server, prior to disconnecting the clients.
248254
/// </summary>

Packages/com.unity.multiplayer.samples.coop/Utilities/SceneManagement/SceneLoaderWrapper.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,16 +88,24 @@ public void LoadScene(string sceneName, LoadSceneMode loadSceneMode = LoadSceneM
8888
}
8989
}
9090

91+
public bool IsClosingClients { get; set; }
92+
9193
void OnSceneLoaded(Scene scene, LoadSceneMode loadSceneMode)
9294
{
93-
if (m_NetworkManager == null || !m_NetworkManager.IsListening || m_NetworkManager.ShutdownInProgress || !m_NetworkManager.NetworkConfig.EnableSceneManagement)
95+
// we're letting networked scene loading handle our loading screen, since there's still some loading happening after unity scene loading is done.
96+
// in case we're offline, then we let unity's scene loader tell us when to turn off the loading screen.
97+
var isOffline = m_NetworkManager == null || !m_NetworkManager.IsListening || m_NetworkManager.ShutdownInProgress || !m_NetworkManager.NetworkConfig.EnableSceneManagement;
98+
// TODO this can be called while we're still in the WaitForSeconds(0.5) while shutting down host side. which means we'll still be in theory connected. Waiting on fix
99+
// for this in MTT-2821
100+
if (isOffline || IsClosingClients)
94101
{
95102
m_ClientLoadingScreen.StopLoadingScreen();
96103
}
97104
}
98105

99106
void OnSceneEvent(SceneEvent sceneEvent)
100107
{
108+
Debug.Log($"on scene event: {sceneEvent.SceneEventType}");
101109
// Only executes on client
102110
if (m_NetworkManager.IsClient)
103111
{

0 commit comments

Comments
 (0)