question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Calls to `ChangeOwnership(NetworkManager.ServerClientId)` and `RemoveOwnership()` doesn't have same effect

See original GitHub issue

Description

While working on #2507, I remarked that calling serverObject.ChangeOwnership(NetworkManager.ServerClientId) doesn’t have the same effect as serverObject.RemoveOwnership(). Moreover, some automated tests use the first one, others the second.

The difference is subtil, but server side, when removing the ownership, the object entry is deleted in NetworkSpawnManager.OwnershipToObjectsTable, while giving ownership back to the server keeps it. Since the ownership is given back to the server in both case, I think we should let the entry in the table. The entry should be deleted only on despawn.

https://github.com/Unity-Technologies/com.unity.netcode.gameobjects/blob/a418eabe59ed5f5e918d1756ee051436d2819828/com.unity.netcode.gameobjects/Runtime/Spawning/NetworkSpawnManager.cs#L197 VS https://github.com/Unity-Technologies/com.unity.netcode.gameobjects/blob/a418eabe59ed5f5e918d1756ee051436d2819828/com.unity.netcode.gameobjects/Runtime/Spawning/NetworkSpawnManager.cs#L259

Shouldn’t RemoveOwnership() just calls ChangeOwnership(NetworkManager.ServerClientId) instead of having a “copy/paste but not identical” implementation? If there is a difference made on purpose between those two calls, documentation needs to be updated to explain it.

Environment

Issue Analytics

  • State:closed
  • Created 5 months ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
NoelStephensUnitycommented, Jul 12, 2023

@PitouGames This issue is resolved in PR #2618 and will be included in the next patch update. 👍

0reactions
PitouGamescommented, Jun 21, 2023

@NoelStephensUnity Thank you for the very detailed answer, and sorry for the delay of my response.

Concerning the automated tests, I want to highlight that you can replace RemoveOwnership with:

internal void RemoveOwnership(NetworkObject networkObject)
{
    ChangeOwnership(networkObject, NetworkManager.ServerClientId);
}

and no test fails, showing that this intentionnal behaviour difference isn’t properly tested. That’s the main reason I have created this issue, I couldn’t know if the difference was intentionnal or not (lack of tests and documentation).

For all the others thing we spooted along the path, it shouldn’t cause any trouble as long as the dev don’t mess up with the two functions as I did for testing. But it’s good to know it’s there 😉

Read more comments on GitHub >

github_iconTop Results From Across the Web

Add an event for every clients when object ownership ...
OnOwnershipChanged() virtual method. When this method get called, ownership data have already been updated, so we can use OwnerClientId property ...
Read more >
Changing ownership doesn't work with Client Network ...
Works fine. When the player drops the box, I remove ownership using RemoveOwnership(). Ownership is returned to the server. Expected - the box ......
Read more >
Unity Netcode: Change Ownership doesn't work for me
1 Answer. The reason this doesn't work is that the server is sending two messages in quick succession.
Read more >
Changelog | Netcode for GameObjects | 1.4.0
OnInitialize virtual method that is invoked after the NetworkTransform has been initialized or re-initialized when ownership changes.
Read more >
Changelog | Netcode for GameObjects | 1.2.0
OnClientConnectedCallback was being invoked before in-scene placed NetworkObject s had been spawned when starting NetworkManager as a host.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found