Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions com.unity.multiplayer.mlapi/Runtime/Core/NetworkManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1508,7 +1508,7 @@ internal void OnClientDisconnectFromServer(ulong clientId)
if (PrefabHandler.ContainsHandler(ConnectedClients[clientId].PlayerObject.GlobalObjectIdHash))
{
PrefabHandler.HandleNetworkPrefabDestroy(ConnectedClients[clientId].PlayerObject);
SpawnManager.OnDespawnObject(ConnectedClients[clientId].PlayerObject.NetworkObjectId, false);
SpawnManager.OnDespawnObject(ConnectedClients[clientId].PlayerObject, false);
}
else
{
Expand All @@ -1526,7 +1526,7 @@ internal void OnClientDisconnectFromServer(ulong clientId)
if (PrefabHandler.ContainsHandler(ConnectedClients[clientId].OwnedObjects[i].GlobalObjectIdHash))
{
PrefabHandler.HandleNetworkPrefabDestroy(ConnectedClients[clientId].OwnedObjects[i]);
SpawnManager.OnDespawnObject(ConnectedClients[clientId].OwnedObjects[i].NetworkObjectId, false);
SpawnManager.OnDespawnObject(ConnectedClients[clientId].OwnedObjects[i], false);
}
else
{
Expand Down
4 changes: 2 additions & 2 deletions com.unity.multiplayer.mlapi/Runtime/Core/NetworkObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -405,9 +405,9 @@ public static void NetworkHide(List<NetworkObject> networkObjects, ulong clientI

private void OnDestroy()
{
if (NetworkManager != null && NetworkManager.SpawnManager != null && NetworkManager.SpawnManager.SpawnedObjects.ContainsKey(NetworkObjectId))
if (NetworkManager != null && NetworkManager.SpawnManager != null && NetworkManager.SpawnManager.SpawnedObjects.TryGetValue(NetworkObjectId, out var networkObject))
{
NetworkManager.SpawnManager.OnDespawnObject(NetworkObjectId, false);
NetworkManager.SpawnManager.OnDespawnObject(networkObject, false);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,15 @@ public void HandleDestroyObject(ulong clientId, Stream stream)
using (var reader = PooledNetworkReader.Get(stream))
{
ulong networkId = reader.ReadUInt64Packed();
NetworkManager.SpawnManager.OnDespawnObject(networkId, true);
if (!NetworkManager.SpawnManager.SpawnedObjects.TryGetValue(networkId, out NetworkObject networkObject))
Comment thread
LukeStampfli marked this conversation as resolved.
{
// This is the same check and log message that happens inside OnDespawnObject, but we have to do it here
// while we still have access to the network ID, otherwise the log message will be less useful.
Debug.LogWarning($"Trying to destroy object {networkId} but it doesn't seem to exist anymore!");
return;
}

NetworkManager.SpawnManager.OnDespawnObject(networkObject, true);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ internal void DespawnObject(NetworkObject networkObject, bool destroyObject = fa
throw new NotServerException("Only server can despawn objects");
}

OnDespawnObject(networkObject.NetworkObjectId, destroyObject);
OnDespawnObject(networkObject, destroyObject);
}

// Makes scene objects ready to be reused
Expand Down Expand Up @@ -498,7 +498,7 @@ internal void ServerDestroySpawnedSceneObjects()
if (NetworkManager.PrefabHandler != null && NetworkManager.PrefabHandler.ContainsHandler(sobj))
{
NetworkManager.PrefabHandler.HandleNetworkPrefabDestroy(sobj);
OnDespawnObject(sobj.NetworkObjectId, false);
OnDespawnObject(sobj, false);
}
else
{
Expand All @@ -522,7 +522,7 @@ internal void DestroyNonSceneObjects()
{
NetworkManager.PrefabHandler.HandleNetworkPrefabDestroy(networkObjects[i]);

OnDespawnObject(networkObjects[i].NetworkObjectId, false);
OnDespawnObject(networkObjects[i], false);
}
else
{
Expand All @@ -546,7 +546,7 @@ internal void DestroySceneObjects()
if (NetworkManager.PrefabHandler.ContainsHandler(networkObjects[i]))
{
NetworkManager.PrefabHandler.HandleNetworkPrefabDestroy(networkObjects[i]);
OnDespawnObject(networkObjects[i].NetworkObjectId, false);
OnDespawnObject(networkObjects[i], false);
}
else
{
Expand Down Expand Up @@ -607,31 +607,38 @@ internal void ClientCollectSoftSyncSceneObjectSweep(NetworkObject[] networkObjec
}
}

internal void OnDespawnObject(ulong networkObjectId, bool destroyGameObject)
internal void OnDespawnObject(NetworkObject networkObject, bool destroyGameObject)
{
if (NetworkManager == null)
{
return;
}

// We have to do this check first as subsequent checks assume we can access NetworkObjectId.
if (networkObject == null)
{
Comment thread
LukeStampfli marked this conversation as resolved.
Debug.LogWarning($"Trying to destroy network object but it is null");
return;
}

// Removal of spawned object
if (!SpawnedObjects.TryGetValue(networkObjectId, out NetworkObject networkObject))
if (!SpawnedObjects.ContainsKey(networkObject.NetworkObjectId))
{
Debug.LogWarning($"Trying to destroy object {networkObjectId} but it doesn't seem to exist anymore!");
Debug.LogWarning($"Trying to destroy object {networkObject.NetworkObjectId} but it doesn't seem to exist anymore!");
return;
}

// Move child NetworkObjects to the root when parent NetworkObject is destroyed
foreach (var spawnedNetObj in SpawnedObjectsList)
{
var (isReparented, latestParent) = spawnedNetObj.GetNetworkParenting();
if (isReparented && latestParent == networkObjectId)
if (isReparented && latestParent == networkObject.NetworkObjectId)
{
spawnedNetObj.gameObject.transform.parent = null;

if (NetworkLog.CurrentLogLevel <= LogLevel.Normal)
{
NetworkLog.LogWarning($"{nameof(NetworkObject)} #{spawnedNetObj.NetworkObjectId} moved to the root because its parent {nameof(NetworkObject)} #{networkObjectId} is destroyed");
NetworkLog.LogWarning($"{nameof(NetworkObject)} #{spawnedNetObj.NetworkObjectId} moved to the root because its parent {nameof(NetworkObject)} #{networkObject.NetworkObjectId} is destroyed");
}
}
}
Expand All @@ -641,7 +648,7 @@ internal void OnDespawnObject(ulong networkObjectId, bool destroyGameObject)
//Someone owns it.
for (int i = networkClient.OwnedObjects.Count - 1; i > -1; i--)
{
if (networkClient.OwnedObjects[i].NetworkObjectId == networkObjectId)
if (networkClient.OwnedObjects[i].NetworkObjectId == networkObject.NetworkObjectId)
{
networkClient.OwnedObjects.RemoveAt(i);
}
Expand All @@ -657,7 +664,7 @@ internal void OnDespawnObject(ulong networkObjectId, bool destroyGameObject)
{
ReleasedNetworkObjectIds.Enqueue(new ReleasedNetworkId()
{
NetworkId = networkObjectId,
NetworkId = networkObject.NetworkObjectId,
ReleaseTime = Time.unscaledTime
});
}
Expand All @@ -673,13 +680,13 @@ internal void OnDespawnObject(ulong networkObjectId, bool destroyGameObject)
var buffer = PooledNetworkBuffer.Get();
using (var writer = PooledNetworkWriter.Get(buffer))
{
writer.WriteUInt64Packed(networkObjectId);
writer.WriteUInt64Packed(networkObject.NetworkObjectId);

var queueItem = new RpcFrameQueueItem
{
UpdateStage = NetworkUpdateStage.PostLateUpdate,
QueueItemType = RpcQueueContainer.QueueItemType.DestroyObject,
NetworkId = networkObjectId,
NetworkId = networkObject.NetworkObjectId,
NetworkBuffer = buffer,
NetworkChannel = NetworkChannel.Internal,
ClientNetworkIds = NetworkManager.ConnectedClientsList.Select(c => c.ClientId).ToArray()
Expand All @@ -699,7 +706,7 @@ internal void OnDespawnObject(ulong networkObjectId, bool destroyGameObject)
if (NetworkManager.PrefabHandler.ContainsHandler(networkObject))
{
NetworkManager.PrefabHandler.HandleNetworkPrefabDestroy(networkObject);
OnDespawnObject(networkObjectId, false);
OnDespawnObject(networkObject, false);
}
else
{
Expand All @@ -710,7 +717,7 @@ internal void OnDespawnObject(ulong networkObjectId, bool destroyGameObject)
// for some reason, we can get down here and SpawnedObjects for this
// networkId will no longer be here, even as we check this at the start
// of the function
if (SpawnedObjects.Remove(networkObjectId))
if (SpawnedObjects.Remove(networkObject.NetworkObjectId))
{
SpawnedObjectsList.Remove(networkObject);
}
Expand Down