com.unity.netcode.gameobjects: Incorrect value for `ulong previous` in `OnOwnershipChanged` on owner side, triggering issue in `NetworkTransform`

Description

In Distributed Authority, there is an issue with the ownership change of NetworkTransform that seems to happen only on the Owner side. The error message is : Non-owner Client-1 is being updated by network tick still!!!!

If the previous owner gets back the ownership, the error stops.

I use a component that detect collision with the object and request the Owner to change the ownership if certain conditions are met, like this:

public void CheckCollisionFor(GameObject otherGameObject)
{
    if(!HasAuthority && /*some other condition*/)
        RequestOwnershipRpc(NetworkManager.LocalClientId);
}

[Rpc(SendTo.Authority)]
void RequestOwnershipRpc(ulong clientId)
{
    if(HasAuthority && /*some other condition*/)
        networkObject.ChangeOwnership(clientId);
}

But I don’t know if the issue only happen when it’s the Owner that use NetworkObject.ChangeOwnership() to another client.

The first collision always trigger the issue, but the next collisions seems to be random and not always.

By digging into the code of NetworkTransform, I found the issue is because InternalInitialization is not called but should be called. InternalInitialization is supposed to update bool CanCommitToTransform, but since it’s not called, the value stays to true and triggers the error.

The issue is here, but more generally on all OnOwnershipChanged I think.

NetworkTransform.cs

protected override void OnOwnershipChanged(ulong previous, ulong current)
{
    // If we were the previous owner or the newly assigned owner then reinitialize
    Debug.Log($"[NetworkTransform] ({gameObject.name}) OnOwnershipChanged   previous={previous}, current={current} : {m_CachedNetworkManager.LocalClientId}", this);
    if (current == m_CachedNetworkManager.LocalClientId || previous == m_CachedNetworkManager.LocalClientId)
    {
        InternalInitialization(true);
    }
    base.OnOwnershipChanged(previous, current);
}

On the first call of OnOwnershipChanged after being spawned the output is :

  • On the owner of the object: previous = 2 current = 2 : m_CachedNetworkManager.LocalClientId = 1
  • On the client that triggers the ownership request : previous = 1 current = 2 : m_CachedNetworkManager.LocalClientId = 2

The Owner doesn’t have the correct previous client id, which should be him. And in the case of NetworkTransform, it doesn’t call InternalInitialization to update the CanCommitToTransform variable.

Environment

  • OS: Windows 11
  • Unity Version: 6000.0.32f1
  • Netcode Version: 2.2.0

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 21

Commits related to this issue

Most upvoted comments

It was the same error, I quickly retested it and I had no problem on the most recent version.

@NoelStephensUnity There are no more errors after several tries.

Except once for a single object but I really pushed with over 1000 objects that appeared very quickly.

But otherwise it’s good.

And for the RPC it’s actually better, I don’t know why I didn’t think of it sooner.

@FureurTime One last note about your RPC. You might think about adjusting it from this:

    [Rpc(SendTo.Authority)]
    void RequestOwnershipRpc(ulong clientId)
    {
        if(!HasAuthority || alreadyTransferringOwnership || !ConditionMet()) return;
        if(Time.time - lastCollisionTime > collisionTimeExpiration)
        {
            networkObject.ChangeOwnership(clientId);
            alreadyTransferringOwnership = true;
            alreadyTransferringOwnershipReset = StartCoroutine(AlreadyTransferingOwnershipReset());
        }
    }

The above basically ends up sending the sender’s identifier twice…(more on this below)

To avoid doing that, you could update it to something like this:

    [Rpc(SendTo.Authority)]
    void RequestOwnershipRpc(RpcParams rpcParams = default)
    {
        if(!HasAuthority || alreadyTransferringOwnership || !ConditionMet()) return;
        if(Time.time - lastCollisionTime > collisionTimeExpiration)
        {
            // Use the already existing sender's client identifier
            networkObject.ChangeOwnership(rpcParams.Receive.SenderClientId);
            alreadyTransferringOwnership = true;
            alreadyTransferringOwnershipReset = StartCoroutine(AlreadyTransferingOwnershipReset());
        }
    }

So when you invoke it from a non-authority side you just need to invoke: RequestOwnershipRpc();

Basically, the RpcParams is something that is created and used internally (whether you specify it as being a parameter to be passed into the Rpc method itself or not). Anyway, it is a minor savings in bandwidth… but figured it was worth mentioning.

@NoelStephensUnity Okay i found the root of the issue, but i will need explaination on why. It make no sense. It was on another component, but this component doesn’t comunicate with my CollisionOwnershipTransfer and doesn’t touch to the ownership. The issue is cause of a NetworkVariable with a class, I didnt test with struct yet.

  • if I set the Value in OnNetworkSpawn and dont touch it, <span style="color:red">it will trigger the error</span>.
  • if I dont touch the Value and dont set the it in OnNetworkSpawn, <span style="color:green">there is no error</span>.

here its if the Value is not set in OnNetworkSpawn, but sometime its inverted:

  • if I set the Value somewhere else 1 time, <span style="color:red">it will trigger the error</span>.
  • if I set the Value somewhere else 2 time or more, <span style="color:green">there is no error</span>.

I think its related to the values inside NetworkVariable, but why does it change the client id given to OnOwnershipChanged ?

I will try to replicate it on the debug projet, and share it when its done.