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

Question (Question/Feedback) How to distinguish client ownership when client is host?

Discussion in 'Netcode for GameObjects' started by ledbetterMRI, May 22, 2023.

  1. ledbetterMRI

    ledbetterMRI

    Joined:
    May 29, 2019
    Posts:
    23
    I have various network objects in a scene that can be picked up and put down. When one is picked up, I'd like to set that objects' ownership to the picker's client Id. If the network object is already owned by a client, I want to prevent any other client from picking up the network object until it is put down. But Netcode automatically assigns network ownership to a client whose machine is also hosting the server (for the host client, when 'IsOwnedByServer' is true, 'IsOwner' is also true). I can't use Netcode's ownership functionality alone to determine who has currently picked up a given network object when the pick-upper is a client on the host machine.

    My current solution uses a network variable boolean to distinguish if the network object's owner is holding the network object. Is this the best way to handle this distinction?

    If feasible, I'd recommend making IsOwnedByServer and IsOwner mutually exclusive. Intuitively, the server owning a network object doesn't necessarily mean the client on the same machine owns it as well. If this change isn't possible, further documentation about host client ownership would help clarify this relationship, as this is not immediately clear in the current docs or API.
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    3,899
    There is also IsHost. The host is the client that is serving on the same machine.
    Seeing some code would help. I think you are missing a detail here, it may be as simple as a branch on IsHost or maybe no branching at all.
     
  3. ledbetterMRI

    ledbetterMRI

    Joined:
    May 29, 2019
    Posts:
    23
    Hey CodeSmile, thanks for taking the time to reply! Lemme see if I can clarify:

    Because all network objects have a non-nullable ownership client ID, this defaults to 0 on network spawn; the first joining client "soft inherits" ownership of network game objects. When using a dedicated server, this is fine; IsOwnedByServer can be used to determine if network objects are truly owned by Client 0 or not. But, when a client machine is also the server, the host client will always be Client 0. In this scenario, server-owned network objects report the following to a host Client 0:
    • IsOwnedByServer = true
    • IsOwner = true
    • OwnerID = 0
    This will always be true for host Client 0, as you mentioned:
    • IsHost = true
    • IsServer = true
    • IsClient = true
    TL;DR: The built-in way to determine network object client ownership vs server ownership doesn't work when hosting the server on a client machine because Netcode doesn't distinguish between host Client 0 and server.
     
    CodeSmile likes this.
  4. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    3,899
    I see. That is indeed unfortunate.

    But maybe the solution is as simple as subclassing NetworkObject (is it sealed?) or adding a component that merely contains IsServerOwned bool to indicate server ownership and it only needs to be checked if the owner's clientId is 0.
     
    ledbetterMRI likes this.
  5. ledbetterMRI

    ledbetterMRI

    Joined:
    May 29, 2019
    Posts:
    23
    Yea that's pretty much what I'm doing currently haha. Hopefully in the future unity will revise netcode for this use-case since its seems pretty common.
     
    Last edited: May 25, 2023
    hawaiian_lasagne likes this.