Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

Networking SyncVar doesn't sync from OnServerAddPlayer

Discussion in '5.1 Beta' started by De-Panther, May 30, 2015.

  1. De-Panther

    De-Panther

    Joined:
    Dec 27, 2009
    Posts:
    589
    I override OnServerAddPlayer of the NetworkManager,
    and from there I call a method in NetworkBehaviour that attched to the added player.
    This method update a SyncVar integer.

    I added an hook method to this SyncVar, and added debug log.
    When I test it as the user, the log of the hook shows that the SyncVar was updated to the new value(1).
    But then on the Start method the SyncVar is back to 0.
    If I look at it from other users - it is 1.

    In other words:
    OnServerAddPlayer.
    Calls SetSyncVarInt(1).
    Calls SyncVarInt=1.
    On SyncVarIntHook the debug log show that SyncVarInt is 1.
    Then Start is called and the debug log show that SyncVarInt is 0.
    If I look at the Inspector SyncVarInt is 0.
    And again, if I look at the Inspector on other users, they show that SyncVarInt is 1.

    I'm a little confused if it should be 0 on all the machines and I should update the value from other call, or is it should be 1.
     
  2. Indiefreaks

    Indiefreaks

    Joined:
    Nov 12, 2012
    Posts:
    89
    SyncVar values should only be set on the server as they are then synchronized with all clients including the one where it actually resides.

    You may want to use a Command on a client networkbehaviour to actually change the SyncVar value.
     
  3. De-Panther

    De-Panther

    Joined:
    Dec 27, 2009
    Posts:
    589
    As I wrote, I override OnServerAddPlayer of the NetworkManager.
    It's a method that runs on the server side.
     
  4. seanr

    seanr

    Unity Technologies

    Joined:
    Sep 22, 2014
    Posts:
    669
    Hook functions are only called for incremental state updates, not for initial state when spawning objects.

    This is deliberate, as hook functions often do things that should not be done for the default values of syncvars.
     
  5. De-Panther

    De-Panther

    Joined:
    Dec 27, 2009
    Posts:
    589
    @seanr
    So how should I aproach it?
    I want that when the player object is added, to define some vars that also other players should know about him(like his team)

    Also - the hook function works, but when the start function is called, the var goes back to the default value
     
  6. AlwaysBeCoding247

    AlwaysBeCoding247

    Joined:
    Jan 27, 2014
    Posts:
    41
    @De-Panther Look in the manual under Object Spawning. There is a basic example of a NetworkBehavior you would set up for an object and accessing it and setting SyncVar data on it before spawning it into the game. Combine that with the NetworkManager manual example for OnServerAddPlayer. That may be what you are looking for
     
    Last edited: May 31, 2015
  7. De-Panther

    De-Panther

    Joined:
    Dec 27, 2009
    Posts:
    589
    @AlwaysBeCoding247 looking at it, It seems that in order to set those vars, I need to set them before calling to "NetworkServer.AddPlayerForConnection".
    But in that case, I can't use "base.OnServerAddPlayer" and do stuff before and after, I need to rewrite it.
     
  8. seanr

    seanr

    Unity Technologies

    Joined:
    Sep 22, 2014
    Posts:
    669
    yes. the base class function doesn't do much.. just some validation and spawn position stuff.

    Code (CSharp):
    1.         public virtual void OnServerAddPlayer(NetworkConnection conn, short playerControllerId)
    2.         {
    3.             if (m_PlayerPrefab == null)
    4.             {
    5.                 if (LogFilter.logError) { Debug.LogError("The PlayerPrefab is empty on the NetworkManager. Please setup a PlayerPrefab object."); }
    6.                 return;
    7.             }
    8.  
    9.             if (m_PlayerPrefab.GetComponent<NetworkIdentity>() == null)
    10.             {
    11.                 if (LogFilter.logError) { Debug.LogError("The PlayerPrefab does not have a NetworkIdentity. Please add a NetworkIdentity to the player prefab."); }
    12.                 return;
    13.             }
    14.  
    15.             if (playerControllerId < conn.playerControllers.Count  && conn.playerControllers[playerControllerId].IsValid && conn.playerControllers[playerControllerId].gameObject != null)
    16.             {
    17.                 if (LogFilter.logError) { Debug.LogError("There is already a player at that playerControllerId for this connections."); }
    18.                 return;
    19.             }
    20.  
    21.             GameObject player;
    22.             Transform startPos = GetStartPosition();
    23.             if (startPos != null)
    24.             {
    25.                 player = (GameObject)GameObject.Instantiate(m_PlayerPrefab, startPos.position, startPos.rotation);
    26.             }
    27.             else
    28.             {
    29.                 player = (GameObject)GameObject.Instantiate(m_PlayerPrefab, Vector3.zero, Quaternion.identity);
    30.             }
    31.            
    32.             NetworkServer.AddPlayerForConnection(conn, player, playerControllerId);
    33.         }
     
  9. De-Panther

    De-Panther

    Joined:
    Dec 27, 2009
    Posts:
    589
    And it won't change over time?