Search Unity

Synced Variable Initialization on Reconnect

Discussion in 'Multiplayer' started by Chronogeran, Jul 13, 2017.

  1. Chronogeran

    Chronogeran

    Joined:
    Jul 10, 2014
    Posts:
    1
    I have a project in a server-client setup, using Unity 5.6.1p4. I would like to continue usage without restarting even after connection errors, while keeping synced values consistent. Specifically, I'm using SyncListStructs.

    I connect the client to the server, shut down and restart the server, and then reconnect to the server on the client without restarting.

    According to this thread, the proper way to read initial values of synced variables on a NetworkBehavior on a client is to use OnStartClient():
    https://forum.unity3d.com/threads/syncvar-hook-not-called-when-client-joins-game.341710/

    But OnStartClient is called only once, not on subsequent connects. NetworkManager's OnClientConnect is called every time, but listening to that is too early; the lists haven't been synced yet. And I don't want to just wait for a certain amount of time, since I might get actual SyncListStruct operation callbacks in the meantime, and doing an initialization after that would cause list elements to get processed for creation multiple times.

    I could add logic to check for duplicates in that situation, but my real question is this:
    Is this kind of use case supposed to be supported in Unity's networking system? If so, what is the proper way to reinitialize values received from a second or subsequent server connection?
     
    Last edited: Jul 14, 2017
  2. Zullar

    Zullar

    Joined:
    May 21, 2013
    Posts:
    651
    Try OnDeserialize(...)
    {
    base.OnDeserialize(...);
    if initialState == true
    {
    //your initialization code here
    }
    }