Search Unity

Discussion Order of callbacks when starting a host

Discussion in 'Netcode for GameObjects' started by ben_carrick, Oct 15, 2022.

  1. ben_carrick

    ben_carrick

    Joined:
    Apr 10, 2020
    Posts:
    2
    Hi everyone,

    I'm trying to get my head around the order of the callbacks. I noticed that when I'm starting a host the order of the callbacks are as follows:

    1. ConnectionApprovalCallback
    2. OnClientConnectedCallback
    3. OnServerStartedCallback

    This is pretty confusing as it looks like the client gets approved and connected before the server has been started. More importantly the network features like RPCs and NetworkVariables are not available when the local client connects which makes it impossible to set up the server environment in the OnServerStarted Callback when starting as a host.

    Is this behaviour intentional or is it a bug?
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,892
    I shall give this a try tomorrow.
    When do you register the callback methods, Start, Awake, OnNetworkSpawn? All in the same script? And when do you call StartHost?
     
  3. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,892
    I get the callbacks in the same order, with another client connected afterwards:
    Code (CSharp):
    1. => ConnectionApproval(by: 0)
    2. => OnClientConnected(0)
    3. => OnServerStarted
    4. => OnClientConnected(0)
    The logged value is the clientId.

    It seems odd on first sight. But I can also see it *may* be intentional considering that when hosting, the server isn't really started unless the host is connected. At least one could argue this way.

    I do wonder about the second client connected. But I believe this is due to schizophrenia. The first may be the server-host receiving the client connected event, the other may be the client-host getting the event, which naturally has to occur AFTER the server started for a client.
     
    Last edited: Oct 16, 2022
  4. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,892
    Correction: the callback #4 was me calling that manually from OnServerStarted. ;)