Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Tracking Ownership in Host Architecture

Discussion in 'Netcode for GameObjects' started by LaneFox, May 24, 2023.

  1. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,384
    Since the Host is the Server in non-dedicated architecture what is the general approach to tracking actual ownership of objects?

    It seems like there is a lot of good value in the Ownership concept built into the NGO system but since the Host will always be the default owner for objects it cannot function as a proper client which becomes difficult/impossible to manage.

    Right now I'm assuming I'll basically need to create some other way to track ownership since I don't want the Host to conceptually own everything. Is that correct?
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    4,026
  3. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,384
    As I read it the Host is clientId = 0 and it's a ulong so yes, the ownerId will be 0 and thus the server owns everything until the OwnerId changes. If you give up ownership as the Host then you get both OnLostOwnership and OnGainedOwnership callbacks. Apparently you cannot relinquish ownership as the Host.

    I'm not sure what to think of IsOwnedByServer. How does that work against IsOwner which is handled by comparing client id? Would it only be true if it was owned-as-a-client?
     
  4. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    4,026
    Is OwnerId and ClientId really the same concept?
    I wonder because ClientId vary and are not guaranteed and can even be re-used. But sure, when hosting it may be so that the hosting client is always guaranteed to have ClientId 0. But if there is a dedicated server that doesn’t mean that the first connected client is ClientId 1, or is it?

    Maybe make a test case if you aren’t 100% certain. I would like to know too.
     
  5. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,384
    Tests:
    • If you spawn an object with ownership for the Host it will return true for both
      IsOwner
      and
      IsOwnedByServer
      on the Host.
    • If you request ownership via RPC from the Host and in that method use
      NetworkObject.ChangeOwner(ServerRpcParams data)
      then nothing seems to happen.
      OnGainedOwnership
      is not fired.
    • If you use
      NetworkObject.RemoveOwnership()
      then
      OnLostOwnership()
      is not fired.
    It does not seem possible to differentiate between wether the object is owned by the Host as a client or if it is owned by the Host as the server since the client id seems to be used to determine ownership. My guess is that internally it's ignoring the call since the owner is already 0. There also does not seem like a way to assign a bogus client id to circumvent this design choice.

    This does not seem like it is working as intended, since it's a pretty significant caveat to not be able to have the Host capable of acting like an actual client in terms of ownership. The provided
    IsOwnedByServer
    is not useful since it is always true on the Host if the owning client is 0.

    It looks like I roll a parallel system for the concept of Authority over objects.
     
    ledbetterMRI likes this.
  6. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,384
    Ironically, this question was also posted this same week.
     
    ledbetterMRI likes this.
  7. ledbetterMRI

    ledbetterMRI

    Joined:
    May 29, 2019
    Posts:
    25
    My current work-around is to add another network boolean variable in the NetworkBehaviour that is false by default and is only set to true when a client is deliberately assigned ownership of the network object. When ownership is lost, this bool goes back to false. Regardless of the ownership ID or IsOwnedByServer value, the network object can use this to distinguish if it's actually owned by a client. Feels redundant, but gets the job done :p
     
  8. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,384
    Nice, that seems like a simple way around it.

    I ended up adding the concept of Authority to the base class for network objects that functions pretty much the same as ownership. It's very redundant since I have the ownership stuff getting changed in parallel (for instance, for netvars with client write privs) but at least it's correct on the host.
     
    ledbetterMRI likes this.