Skip to content

Commit 26f2a80

Browse files
committed
adding wait for next frame to check for gameover when client disconnects + cleaning up
1 parent f74f736 commit 26f2a80

File tree

1 file changed

+11
-17
lines changed

1 file changed

+11
-17
lines changed

Assets/BossRoom/Scripts/Server/Game/State/ServerBossRoomState.cs

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public override void OnNetworkSpawn()
7777
DoInitialSpawnIfPossible();
7878

7979
SessionManager<SessionPlayerData>.Instance.OnSessionStarted();
80-
m_LifeStateChangedEventMessageSubscriber.Subscribe(OnLifeStateChangedEventMessage);
80+
m_Subscription = m_LifeStateChangedEventMessageSubscriber.Subscribe(OnLifeStateChangedEventMessage);
8181
}
8282
}
8383

@@ -105,10 +105,17 @@ void OnClientDisconnect(ulong clientId)
105105
if (clientId != NetworkManager.LocalClientId)
106106
{
107107
// If a client disconnects, check for game over in case all other players are already down
108-
CheckForGameOver();
108+
StartCoroutine(WaitToCheckForGameOver());
109109
}
110110
}
111111

112+
IEnumerator WaitToCheckForGameOver()
113+
{
114+
// Wait until next frame so that the client's player character has despawned
115+
yield return null;
116+
CheckForGameOver();
117+
}
118+
112119
public void OnClientSceneChanged(SceneEvent sceneEvent)
113120
{
114121
if (sceneEvent.SceneEventType != SceneEventType.LoadComplete) return;
@@ -140,25 +147,12 @@ public override void OnNetworkDespawn()
140147
{
141148
if (m_NetPortal != null)
142149
{
150+
NetworkManager.OnClientDisconnectCallback -= OnClientDisconnect;
143151
NetworkManager.SceneManager.OnSceneEvent -= OnClientSceneChanged;
144152
}
145153
m_Subscription?.Dispose();
146154
}
147155

148-
/// <summary>
149-
/// Helper method for OnDestroy that gets the NetworkLifeState.OnValueChanged event for a NetworkObjectId, or null if it doesn't exist.
150-
/// </summary>
151-
private NetworkVariable<LifeState>.OnValueChangedDelegate GetLifeStateEvent(ulong id)
152-
{
153-
//this is all a little paranoid, because during shutdown it's not always obvious what state is still valid.
154-
if (NetworkManager != null && NetworkManager.SpawnManager != null && NetworkManager.SpawnManager.SpawnedObjects.TryGetValue(id, out NetworkObject netObj) && netObj != null)
155-
{
156-
var netState = netObj.GetComponent<NetworkCharacterState>();
157-
return netState != null ? netState.NetworkLifeState.LifeState.OnValueChanged : null;
158-
}
159-
return null;
160-
}
161-
162156
private void SpawnPlayer(ulong clientId, bool lateJoin)
163157
{
164158
Transform spawnPoint = null;
@@ -259,7 +253,7 @@ void CheckForGameOver()
259253
// Check the life state of all players in the scene
260254
foreach (var serverCharacter in PlayerServerCharacter.GetPlayerServerCharacters())
261255
{
262-
// if any player is alive just retun
256+
// if any player is alive just return
263257
if (serverCharacter.NetState && serverCharacter.NetState.LifeState == LifeState.Alive)
264258
{
265259
return;

0 commit comments

Comments
 (0)