Skip to content

Commit d563072

Browse files
fix: Ownership table entries not being removed on clients [MTT-2974] (#1838)
* fix Spawn manager not removing entries from ownership table * Update CHANGELOG.md MTT-2974 * fix Removing the ownership check in InvokeBehaviourOnLostOwnership because clients still need to update their table when the owner has changed to remove their ownership entry in their local ownership table. * test updating the integration test to check and make sure that the OwnershipToObjectsTable is having entries removed when ownership is lost. * update removing some debug code * update adding PR number
1 parent ea2ef99 commit d563072

File tree

4 files changed

+16
-4
lines changed

4 files changed

+16
-4
lines changed

com.unity.netcode.gameobjects/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Additional documentation and release notes are available at [Multiplayer Documen
2121
- Removed `com.unity.modules.animation`, `com.unity.modules.physics` and `com.unity.modules.physics2d` dependencies from the package (#1812)
2222

2323
### Fixed
24+
- Fixed issue where entries were not being removed from the NetworkSpawnManager.OwnershipToObjectsTable. (#1838)
2425
- Fixed clarity for NetworkSceneManager client side notification when it receives a scene hash value that does not exist in its local hash table. (#1828)
2526
- Fixed client throws a key not found exception when it times out using UNet or UTP. (#1821)
2627
- Fixed network variable updates are no longer limited to 32,768 bytes when NetworkConfig.EnsureNetworkVariableLengthSafety is enabled. The limits are now determined by what the transport can send in a message. (#1811)

com.unity.netcode.gameobjects/Runtime/Core/NetworkObject.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -515,8 +515,8 @@ public void ChangeOwnership(ulong newOwnerClientId)
515515

516516
internal void InvokeBehaviourOnLostOwnership()
517517
{
518-
// Server already handles this earlier, hosts should ignore
519-
if (!NetworkManager.IsServer && NetworkManager.LocalClientId == OwnerClientId)
518+
// Server already handles this earlier, hosts should ignore, all clients should update
519+
if (!NetworkManager.IsServer)
520520
{
521521
NetworkManager.SpawnManager.UpdateOwnershipTable(this, OwnerClientId, true);
522522
}
@@ -529,7 +529,7 @@ internal void InvokeBehaviourOnLostOwnership()
529529

530530
internal void InvokeBehaviourOnGainedOwnership()
531531
{
532-
// Server already handles this earlier, hosts should ignore
532+
// Server already handles this earlier, hosts should ignore and only client owners should update
533533
if (!NetworkManager.IsServer && NetworkManager.LocalClientId == OwnerClientId)
534534
{
535535
NetworkManager.SpawnManager.UpdateOwnershipTable(this, OwnerClientId);

com.unity.netcode.gameobjects/Runtime/Spawning/NetworkSpawnManager.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,10 @@ internal void UpdateOwnershipTable(NetworkObject networkObject, ulong newOwner,
113113
networkObject.InvokeBehaviourOnGainedOwnership();
114114
}
115115
}
116+
else if (isRemoving)
117+
{
118+
OwnershipToObjectsTable[previousOwner].Remove(networkObject.NetworkObjectId);
119+
}
116120
else if (NetworkManager.LogLevel == LogLevel.Developer)
117121
{
118122
NetworkLog.LogWarning($"Setting ownership twice? Client-ID {previousOwner} already owns NetworkObject ID {networkObject.NetworkObjectId}!");

com.unity.netcode.gameobjects/Tests/Runtime/NetworkObject/NetworkObjectOwnershipTests.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,18 +192,25 @@ public IEnumerator TestOwnershipCallbacksSeveralClients()
192192
Assert.That(m_ServerNetworkManager.ConnectedClients.ContainsKey(clientId));
193193
serverObject.ChangeOwnership(clientId);
194194
yield return s_DefaultWaitForTick;
195-
196195
Assert.That(serverComponent.OnLostOwnershipFired);
197196
Assert.That(serverComponent.OwnerClientId, Is.EqualTo(clientId));
198197
// Wait for all clients to receive the CreateObjectMessage
199198
yield return WaitForConditionOrTimeOut(ownershipMessageHooks);
200199
Assert.False(s_GlobalTimeoutHelper.TimedOut, $"Timed out waiting for all clients to receive the {nameof(ChangeOwnershipMessage)} message.");
201200

201+
var previousNetworkManager = m_ServerNetworkManager;
202202
if (previousClientComponent != null)
203203
{
204+
// Once we have a previousClientComponent, we want to verify the server is keeping track for the removal of ownership in the OwnershipToObjectsTable
205+
Assert.That(!m_ServerNetworkManager.SpawnManager.OwnershipToObjectsTable[m_ServerNetworkManager.LocalClientId].ContainsKey(serverObject.NetworkObjectId));
206+
previousNetworkManager = previousClientComponent.NetworkManager;
204207
Assert.That(previousClientComponent.OnLostOwnershipFired);
205208
Assert.That(previousClientComponent.OwnerClientId, Is.EqualTo(clientId));
206209
}
210+
211+
// Assure the previous owner is no longer in the local table of the previous owner.
212+
Assert.That(!previousNetworkManager.SpawnManager.OwnershipToObjectsTable[previousNetworkManager.LocalClientId].ContainsKey(serverObject.NetworkObjectId));
213+
207214
var currentClientComponent = clientObjects[clientIndex].GetComponent<NetworkObjectOwnershipComponent>();
208215
Assert.That(currentClientComponent.OnGainedOwnershipFired);
209216

0 commit comments

Comments
 (0)