Skip to content

fix: Ownership table entries not being removed on clients [MTT-2974] #1838

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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
1 change: 1 addition & 0 deletions com.unity.netcode.gameobjects/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Additional documentation and release notes are available at [Multiplayer Documen
- Removed `com.unity.modules.animation`, `com.unity.modules.physics` and `com.unity.modules.physics2d` dependencies from the package (#1812)

### Fixed
- Fixed issue where entries were not being removed from the NetworkSpawnManager.OwnershipToObjectsTable. (#1838)
- Fixed clarity for NetworkSceneManager client side notification when it receives a scene hash value that does not exist in its local hash table. (#1828)
- Fixed client throws a key not found exception when it times out using UNet or UTP. (#1821)
- 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)
Expand Down
6 changes: 3 additions & 3 deletions com.unity.netcode.gameobjects/Runtime/Core/NetworkObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -515,8 +515,8 @@ public void ChangeOwnership(ulong newOwnerClientId)

internal void InvokeBehaviourOnLostOwnership()
{
// Server already handles this earlier, hosts should ignore
if (!NetworkManager.IsServer && NetworkManager.LocalClientId == OwnerClientId)
// Server already handles this earlier, hosts should ignore, all clients should update
if (!NetworkManager.IsServer)
{
NetworkManager.SpawnManager.UpdateOwnershipTable(this, OwnerClientId, true);
}
Expand All @@ -529,7 +529,7 @@ internal void InvokeBehaviourOnLostOwnership()

internal void InvokeBehaviourOnGainedOwnership()
{
// Server already handles this earlier, hosts should ignore
// Server already handles this earlier, hosts should ignore and only client owners should update
if (!NetworkManager.IsServer && NetworkManager.LocalClientId == OwnerClientId)
{
NetworkManager.SpawnManager.UpdateOwnershipTable(this, OwnerClientId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ internal void UpdateOwnershipTable(NetworkObject networkObject, ulong newOwner,
networkObject.InvokeBehaviourOnGainedOwnership();
}
}
else if (isRemoving)
{
OwnershipToObjectsTable[previousOwner].Remove(networkObject.NetworkObjectId);
}
else if (NetworkManager.LogLevel == LogLevel.Developer)
{
NetworkLog.LogWarning($"Setting ownership twice? Client-ID {previousOwner} already owns NetworkObject ID {networkObject.NetworkObjectId}!");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,18 +192,25 @@ public IEnumerator TestOwnershipCallbacksSeveralClients()
Assert.That(m_ServerNetworkManager.ConnectedClients.ContainsKey(clientId));
serverObject.ChangeOwnership(clientId);
yield return s_DefaultWaitForTick;

Assert.That(serverComponent.OnLostOwnershipFired);
Assert.That(serverComponent.OwnerClientId, Is.EqualTo(clientId));
// Wait for all clients to receive the CreateObjectMessage
yield return WaitForConditionOrTimeOut(ownershipMessageHooks);
Assert.False(s_GlobalTimeoutHelper.TimedOut, $"Timed out waiting for all clients to receive the {nameof(ChangeOwnershipMessage)} message.");

var previousNetworkManager = m_ServerNetworkManager;
if (previousClientComponent != null)
{
// Once we have a previousClientComponent, we want to verify the server is keeping track for the removal of ownership in the OwnershipToObjectsTable
Assert.That(!m_ServerNetworkManager.SpawnManager.OwnershipToObjectsTable[m_ServerNetworkManager.LocalClientId].ContainsKey(serverObject.NetworkObjectId));
previousNetworkManager = previousClientComponent.NetworkManager;
Assert.That(previousClientComponent.OnLostOwnershipFired);
Assert.That(previousClientComponent.OwnerClientId, Is.EqualTo(clientId));
}

// Assure the previous owner is no longer in the local table of the previous owner.
Assert.That(!previousNetworkManager.SpawnManager.OwnershipToObjectsTable[previousNetworkManager.LocalClientId].ContainsKey(serverObject.NetworkObjectId));

var currentClientComponent = clientObjects[clientIndex].GetComponent<NetworkObjectOwnershipComponent>();
Assert.That(currentClientComponent.OnGainedOwnershipFired);

Expand Down