Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Any reason isClient, hasAuthority etc are set after OnStartX?

Discussion in 'Multiplayer' started by Iron-Warrior, Apr 30, 2016.

  1. Iron-Warrior

    Iron-Warrior

    Joined:
    Nov 3, 2009
    Posts:
    838
    Not the best title, I admit.

    Anyways, when spawning a new object with a NetworkBehaviour, you have a bunch of virtual methods you can override. OnStartServer, OnStartClient, OnStartAuthority. These are all fine and dandy, but it seems the network behaviour properties are actually set after some of these methods are called. For example, let's say you spawn an object with client authority, for the local client (host). So it will have:
    • isServer = true
    • isClient = true
    • hasAuthority = true
    However, if you run this:

    Code (csharp):
    1.  
    2. public override void OnStartServer()
    3.     {
    4.         Debug.Log("OnStartServer:" + isClient);
    5.     }
    You get false! This is because isServer, isClient and hasAuthority are all set just before their respective virtual methods. Is there any reason these properties aren't set before all the virtual methods are called? I can't think of any reason you wouldn't want to have all this information immediately, and weird as it sounds it's tough to think of a workaround (I need to know if an object has authority in OnStartServer).

    Erik
     
  2. Wriggler

    Wriggler

    Joined:
    Jun 7, 2013
    Posts:
    133
    Totally agree. This has tripped me up as well.

    Ben
     
    isidro02139 likes this.
  3. Oshroth

    Oshroth

    Joined:
    Apr 28, 2014
    Posts:
    99
    On the server, isServer is set and then OnStartServer() is called for each network behaviour, then NetworkIdentity.OnStartClient() is called which sets isClient and calls OnStartClient() for each network behaviour, then it checks if hasAuthority is set and calls NetworkIdentity.OnStartAuthority() if it is. It looks like hasAuthority is set before NetworkBehaviour.OnStartServer() is called. I generally check NetworkServer.active/localClientActive and NetworkClient.active to see if I'm on the server or not
    NetworkIdentity.OnStartServer() abridged source:
    Code (CSharp):
    1. internal void OnStartServer(bool allowNonZeroNetId)
    2.         {
    3.             if (m_IsServer)
    4.             {
    5.                 return;
    6.             }
    7.             m_IsServer = true;
    8.  
    9.             if (m_LocalPlayerAuthority)
    10.             {
    11.                 // local player on server has NO authority
    12.                 m_HasAuthority = false;
    13.             }
    14.             else
    15.             {
    16.                 // enemy on server has authority
    17.                 m_HasAuthority = true;
    18.             }
    19.  
    20.             for (int i = 0; i < m_NetworkBehaviours.Length; i++)
    21.             {
    22.                 NetworkBehaviour comp = m_NetworkBehaviours[i];
    23.                comp.OnStartServer();
    24.             }
    25.  
    26.             if (NetworkClient.active && NetworkServer.localClientActive)
    27.             {
    28.                 // there will be no spawn message, so start the client here too
    29.                OnStartClient();
    30.             }
    31.  
    32.             if (m_HasAuthority)
    33.             {
    34.                 OnStartAuthority();
    35.             }
    36.         }
     
    isidro02139 likes this.
  4. angusmf

    angusmf

    Joined:
    Jan 19, 2015
    Posts:
    261
    I think the section where authority is set based on m_LocalPlayerAuthority is a bug and a holdover from the very early days of UNET when you couldn't add and remove authority from non-player objects. The only practical effect is that OnStartAuthority and OnStopAuthority are not called at the appropriate times by ForceAuthority, but they do nothing by default.
     
    ManAmazin likes this.
  5. angusmf

    angusmf

    Joined:
    Jan 19, 2015
    Posts:
    261
    I submitted a PR to fix this today. There's code in some of the OnStartAuthority overrides that probably needs to be run. If nothing else, it lets you use hasAuthority as you would expect.
     
    Deleted User likes this.