Search Unity

Bug On several Connections at the same time, the Server wont Spawn all Clients correctly

Discussion in 'Netcode for GameObjects' started by itisMarcii_, Jun 13, 2022.

  1. itisMarcii_

    itisMarcii_

    Joined:
    Apr 5, 2022
    Posts:
    111
    It seems like, if you spawn several Clients at the same time, the Server runs into problems.

    One for example is on a low tickrate:

    I spawned 35 Clients on a tickrate of 8, 30 Clients spawned without problems.
    The 5 Clients that spawned at last, couldnt detect the IsLocalPlayer/IsOwner/... booleans.
    I run this in update on a Client:
    Code (CSharp):
    1.     void Update()
    2.     {
    3.       if(IsLocalPlayer) Debug.Log($" My Client: {NetworkManager.LocalClientId}");
    4.       Debug.Log($"Client: {NetworkManager.LocalClientId}");
    5.     }
    And it couldnt detect itself as LocalPlayer while the other 30 could.


    I also spawned 35 Clients on a tickrate of 32, all 35 Clients spawned without this problem.



    There is also the problem with just 2 Clients spawning at the same time (this problem is also valid in the example above):

    One of them will throw this error before the connection is actually finished.

    (this error will occur more often if more clients spawn)

    with the following Error after it spawned:


    While the other is perfectly fine.
    I spawned them from a bash script in batchmode. The server and client are also detached with independent Builds. I use a devel server to connect.
     
  2. CosmoM

    CosmoM

    Joined:
    Oct 31, 2015
    Posts:
    204
    I don't know about the second error, but for the first I find that it's sometimes an issue of timing, where Update can be called before the object is properly network-initialized (which means that variables such as
    IsLocalPlayer
    are not yet set). In such cases, I use a
    bool wasInitialized
    (which defaults to false) which I set to true in
    public override void OnNetworkSpawn()
    . At the top of Update(), I put
    if (!wasInitialized) return;
    , and I set
    wasInitialized=false;
    in OnDisable() for pooled objects.
     
  3. itisMarcii_

    itisMarcii_

    Joined:
    Apr 5, 2022
    Posts:
    111
    I would agree if the Client wouldnt be Spawned from the Server. OnNetworkSpawn should be called pre Update, since this Method gets called before Start. And I tested two variants to Setup all necessary Information on the Client.

    1. From the server itself with the OnConnectionCallback, where i get the ClientID as identifier
    2. OnNetworkSpawn on the Client itself that Requests Information from the Server.

    (The Update was just a test to get a constant result on IsLocalPlayer after the spawn)

    On both i got the same problem. And if i do not mistake, what could be the case, the ClientID is transferred pre Spawn.
     
  4. unity_BYGn4Kt3FQ0mxg

    unity_BYGn4Kt3FQ0mxg

    Joined:
    Jan 14, 2019
    Posts:
    1
    Did you ever resolve this issue? I have the same problem. I have a matchmaking system which signals the clients to join a server. Having a random delay before connecting works, but is not nice. Still trying to find the correct solution to this.