Search Unity

SyncVars - How come this works, but this doesn't?

Discussion in 'Multiplayer' started by LootlabGames, Feb 22, 2018.

  1. LootlabGames

    LootlabGames

    Joined:
    Nov 21, 2014
    Posts:
    343
    I am trying to initialize sync vars on the start from saved data.

    If i use this it will work fine, but I have tons of values to initialize.
    Creating a command for each one seems crazy.

    Code (CSharp):
    1.  
    2. public class Character: NetworkBehaviour
    3. {
    4.     [SyncVar]
    5.     public string characterName;
    6.     [SyncVar]
    7.     public int characterLevel;
    8.     [SyncVar]
    9.     public float health;
    10.     [SyncVar]
    11.     public float maxHealth;
    12.     [SyncVar]
    13.     public float energy;
    14.     [SyncVar]
    15.     public float maxEnergy;
    16.     [SyncVar]
    17.     public float castingSpeed;
    18.     [SyncVar]
    19.     public int status;
    20.     [SyncVar]
    21.     public int characterType;
    22.  
    23.     public override void OnStartAuthority()
    24.     {
    25.         base.OnStartAuthority();
    26.  
    27.         ApplyCharacterData(GameData.instance.playerData);
    28.     }
    29.      
    30.     public void ApplyCharacterData(CharacterData characterData)
    31.     {
    32.         CmdIntName(characterData.characterName);
    33.         CmdIntLevel(characterData.characterLevel);
    34.         CmdIntHealth(characterData.currentHealth);
    35.         CmdIntMaxHealth(characterData.maxHealth);
    36.  
    37.         CmdIntEnergy(characterData.currentEnergy);
    38.         CmdIntMaxEnergy(characterData.maxEnergy);
    39.  
    40.         CmdIntCastingSpeed(characterData.castingSpeed);
    41.         CmdIntStatus((int)characterData.status);
    42.  
    43.         CmdIntCharacterType((int)characterData.characterType);
    44.     }
    45.  
    46.     #region CharacterSyncCommands
    47.  
    48.     [Command]
    49.     public void CmdIntName(string characterName)
    50.     {
    51.         this.characterName = characterName;
    52.     }
    53.  
    54.     [Command]
    55.     public void CmdIntLevel(int characterLevel)
    56.     {
    57.         this.characterLevel = characterLevel;
    58.     }
    59.  
    60.     [Command]
    61.     public void CmdIntHealth(float health)
    62.     {
    63.         this.health = health;
    64.     }
    65.  
    66.     [Command]
    67.     public void CmdIntMaxHealth(float maxHealth)
    68.     {
    69.         this.maxHealth = maxHealth;
    70.     }
    71.  
    72.     [Command]
    73.     public void CmdIntEnergy(float energy)
    74.     {
    75.         this.energy = energy;
    76.     }
    77.  
    78.     [Command]
    79.     public void CmdIntMaxEnergy(float maxEnergy)
    80.     {
    81.         this.maxEnergy = maxEnergy;
    82.     }
    83.  
    84.     [Command]
    85.     public void CmdIntCastingSpeed(float castingSpeed)
    86.     {
    87.         this.castingSpeed = castingSpeed;
    88.     }
    89.  
    90.     [Command]
    91.     public void CmdIntStatus(int status)
    92.     {
    93.         this.status = status;
    94.     }
    95.  
    96.     [Command]
    97.     public void CmdIntCharacterType(int characterType)
    98.     {
    99.         this.characterType = characterType;
    100.     }
    101.     #endregion
    102. }
    103.  
    But this does not work correctly. Some data is populated but not all of it.

    Code (CSharp):
    1.  
    2. public class Character: NetworkBehaviour
    3. {
    4.     [SyncVar]
    5.     public string characterName;
    6.     [SyncVar]
    7.     public int characterLevel;
    8.     [SyncVar]
    9.     public float health;
    10.     [SyncVar]
    11.     public float maxHealth;
    12.     [SyncVar]
    13.     public float energy;
    14.     [SyncVar]
    15.     public float maxEnergy;
    16.     [SyncVar]
    17.     public float castingSpeed;
    18.     [SyncVar]
    19.     public int status;
    20.     [SyncVar]
    21.     public int characterType;
    22.  
    23.     public override void OnStartAuthority()
    24.     {
    25.         base.OnStartAuthority();
    26.  
    27.         ApplyCharacterData(GameData.instance.playerData);
    28.     }
    29.      
    30.     public void ApplyCharacterData(CharacterData characterData)
    31.     {
    32.         PlayerInitMessage initMessage = new PlayerInitMessage()
    33.         {
    34.             currentHealth = characterData.currentHealth,
    35.             maxHealth = characterData.maxHealth,
    36.             currentEnergy = characterData.currentEnergy,
    37.             maxEnergy = characterData.maxEnergy,
    38.  
    39.             castingSpeed = characterData.castingSpeed,
    40.             status = (int)characterData.status,
    41.             characterType = (int)characterData.characterType
    42.         };
    43.      
    44.         CmdIntMainData(initMessage);
    45.     }
    46.  
    47.     #region CharacterSyncCommands
    48.     [Command]
    49.     public void CmdIntMainData(PlayerInitMessage initMessage)
    50.     {
    51.         this.health = initMessage.currentHealth;
    52.         this.MaxHealth = initMessage.maxHealth;
    53.         this.energy = initMessage.currentEnergy;
    54.         this.maxEnergy = initMessage.maxEnergy;
    55.  
    56.         this.castingSpeed = initMessage.castingSpeed;
    57.         this.status = initMessage.status;
    58.         this.characterType = initMessage.characterType;
    59.     }
    60.  
    61.     #endregion
    62. }
    63.  
    64.     public struct PlayerInitMessage
    65.     {
    66.         public string characterName;
    67.         public int characterLevel;
    68.         public float currentHealth;
    69.         public float maxHealth;
    70.         public float currentEnergy;
    71.         public float maxEnergy;
    72.         public float castingSpeed;
    73.         public int status;
    74.         public int characterType;
    75.     }
    76.  
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    In you're 2nd one you're not even setting characterName or characterLevel in your struct, but you are sending them in your first example. If that isn't your issue, please explain what you mean by "Some data is populated but not all of it", because as it is you're not even trying to send the same amount of data in the 2nd example so of course that data wouldn't be populated.
     
  3. LootlabGames

    LootlabGames

    Joined:
    Nov 21, 2014
    Posts:
    343
    Thanks for your reply.
    It appears it was a problem with my code.
    I was assigning the value via a property and the property was changing it to an incorrect value.
     
  4. PrimeDerektive

    PrimeDerektive

    Joined:
    Dec 13, 2009
    Posts:
    3,090
    Commands could have multiple arguments... why not make it one big command?