Search Unity

Question Exception thrown when changing owner.

Discussion in 'Netcode for GameObjects' started by TestifyNow, Sep 30, 2022.

  1. TestifyNow

    TestifyNow

    Joined:
    Feb 9, 2016
    Posts:
    24
    Hey guys, need help with this as I'm struggling badly for days.

    I have client-authoritative movement in my game. My player has Client Network Transform attached.
    I have pick-upable swords in my game, they also have Client Network Transform.

    When player picks-up sword it works as it should, gets synced properly. I'm just setting position and rotation on the sword client-side.

    The problem occurs when another player picks-up the said sword after it was dropped.
    When I'm changing owner, it throws this exception

    Client wrote to {typeof(NetworkVariable<>).Name} without permission. No more variables can be read. This is critical.

    As you can imagine, I am a little confused, because I don't use NetworkVariables. However, what I'm guessing is happening is when I'm changing the owner, the sword was still syncing rotation and position (it has rigidbody, so it falls, so it still gets updated), and when the server was getting those updates without someone in authority, it threw the exception.

    The code itself for changing ownership is very straightforward

    Code (CSharp):
    1. [ServerRpc]
    2. public void ChangeOwnershipServerRpc(NetworkObjectReference reference, ServerRpcParams serverRpcParams = default)
    3. {
    4.     if (reference.TryGet(out var networkObject))
    5.     {
    6.         networkObject.ChangeOwnership(serverRpcParams.Receive.SenderClientId);
    7.     }
    8. }
    I'm looking for a workaround of some sort. Feel free to ask any questions as I'm not the best at explaining.

    Thanks in advance.

    PS. Despite it being 'critical error' it doesn't seem to break anything. (although I have not yet found a way to simulate ping)
     
    Last edited: Sep 30, 2022
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,953
    I guess the NetworkVariable error comes from the (Client)NetworkTransform, assuming it uses NetworkVariables internally.

    My hunch is that the error is thrown because of the change in ownership not being synchronized correctly, ie it might happen that the other client picks up the sword and tries to send out the ClientNetworkTransform stuff before the ChangeOwnership RPC arrives. So you may need to check whether the client already owns that sword before allowing the client to move it. Or the other way around, ie previous client still uses the ClientNetworkTransform even though ownership was already transferred.
     
  3. TestifyNow

    TestifyNow

    Joined:
    Feb 9, 2016
    Posts:
    24
    I don't think this can be feasibly done, let me explain:

    This would work fine, but the client gets the update that he lost ownership frames later after he already sent the messages to the host. That's the problem.

    I don't think there's a way to discard those incoming messages, other than those exceptions being thrown. Unless I'm wrong.

    I wish I was wrong, but it seems right now changing ownership on clientnetworktransform is just not entirely supported, so there are some under-the-hood issues. I really hope they fix them, because my game is heavily based on this concept.
     
  4. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,953
    There's always the workaround where you spawn a new item for the new owner and remove the previous one. ;)