Skip to content

Commit

Permalink
fix: exception thrown when rpc received for depsawned object (back-po…
Browse files Browse the repository at this point in the history
…rt) (#3055)

* fix

We should not throw an exception if an RPC is received and the target NetworkObject does not exist as it could have been despawned.

* update

Adding changelog entry

* update

adding changelog entry PR number.
  • Loading branch information
NoelStephensUnity authored Sep 9, 2024
1 parent 67b27b7 commit 39818f2
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
1 change: 1 addition & 0 deletions com.unity.netcode.gameobjects/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Additional documentation and release notes are available at [Multiplayer Documen

### Fixed

- Fixed issue where an exception could occur when receiving a universal RPC for a `NetworkObject` that has been despawned. (#3055)
- Fixed issue where setting a prefab hash value during connection approval but not having a player prefab assigned could cause an exception when spawning a player. (#3046)
- Fixed issue where collections v2.2.x was not supported when using UTP v2.2.x within Unity v2022.3. (#3033)
- Fixed issue where the `NetworkSpawnManager.HandleNetworkObjectShow` could throw an exception if one of the `NetworkObject` components to show was destroyed during the same frame. (#3029)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System;
using Unity.Collections;

namespace Unity.Netcode
Expand Down Expand Up @@ -35,7 +34,13 @@ public unsafe void Handle(ref NetworkContext context)
var networkManager = (NetworkManager)context.SystemOwner;
if (!networkManager.SpawnManager.SpawnedObjects.TryGetValue(WrappedMessage.Metadata.NetworkObjectId, out var networkObject))
{
throw new InvalidOperationException($"An RPC called on a {nameof(NetworkObject)} that is not in the spawned objects list. Please make sure the {nameof(NetworkObject)} is spawned before calling RPCs.");
// If the NetworkObject no longer exists then just log a warning when developer mode logging is enabled and exit.
// This can happen if NetworkObject is despawned and a client sends an RPC before receiving the despawn message.
if (networkManager.LogLevel == LogLevel.Developer)
{
NetworkLog.LogWarning($"[{WrappedMessage.Metadata.NetworkObjectId}, {WrappedMessage.Metadata.NetworkBehaviourId}, {WrappedMessage.Metadata.NetworkRpcMethodId}] An RPC called on a {nameof(NetworkObject)} that is not in the spawned objects list. Please make sure the {nameof(NetworkObject)} is spawned before calling RPCs.");
}
return;
}

var observers = networkObject.Observers;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,13 @@ public static void Handle(ref NetworkContext context, ref RpcMetadata metadata,
var networkManager = (NetworkManager)context.SystemOwner;
if (!networkManager.SpawnManager.SpawnedObjects.TryGetValue(metadata.NetworkObjectId, out var networkObject))
{
throw new InvalidOperationException($"An RPC called on a {nameof(NetworkObject)} that is not in the spawned objects list. Please make sure the {nameof(NetworkObject)} is spawned before calling RPCs.");
// If the NetworkObject no longer exists then just log a warning when developer mode logging is enabled and exit.
// This can happen if NetworkObject is despawned and a client sends an RPC before receiving the despawn message.
if (networkManager.LogLevel == LogLevel.Developer)
{
NetworkLog.LogWarning($"[{metadata.NetworkObjectId}, {metadata.NetworkBehaviourId}, {metadata.NetworkRpcMethodId}] An RPC called on a {nameof(NetworkObject)} that is not in the spawned objects list. Please make sure the {nameof(NetworkObject)} is spawned before calling RPCs.");
}
return;
}
var networkBehaviour = networkObject.GetNetworkBehaviourAtOrderIndex(metadata.NetworkBehaviourId);

Expand Down

0 comments on commit 39818f2

Please sign in to comment.