Search Unity

[SyncVar] Some glitches or something else

Discussion in 'Multiplayer' started by DrunkGodfather, Mar 17, 2019.

  1. DrunkGodfather

    DrunkGodfather

    Joined:
    Jan 26, 2018
    Posts:
    3
    Hi there. I've got strange logic with sync vars.
    I try to explain. I've got 2 sync vars.
    int speed = 15;
    int health = 100;

    On server it's always good. It's math correct and etc.
    But when connected new client - he got strange values of that vars, like
    int speed = 187;
    int health = 220;

    All commands and rpc's and all math logic work good at all clients and server. There only a glitch with correct sync values at startup.

    Intresting that only client can't get right sync values. Maybe someone knows whats wrong with conncection or something?
     
  2. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    You would have to post the code to take a look
     
    Last edited: Mar 18, 2019
  3. DrunkGodfather

    DrunkGodfather

    Joined:
    Jan 26, 2018
    Posts:
    3
    Code (CSharp):
    1. public class Charecter : NetworkBehaviour {
    2.  
    3.     [SyncVar(hook = "OnSyncSpeedUpdate")]
    4.     public int syncRunSpeed = 15;
    5.  
    6.     [SyncVar(hook = "OnSyncHealthUpdate")]
    7.     public int syncHealth = 100;
    8.  
    9.     [SyncVar(hook = "OnSyncCriticalDamageUpdate")]
    10.     private bool syncCriticalDamage = false;
    11.  
    12.     public int maxHealth = 100;
    13.  
    14.     public GameObject defaultWeapon = null;
    15.  
    16.     private Animator animator = null;
    17.     private UIMessages messages = null;
    18.  
    19.    
    20.     public void OnSyncSpeedUpdate(int runSpeed) {
    21.         LG.log("SYNC OnSyncSpeedUpdate :: " + runSpeed);
    22.         syncRunSpeed = runSpeed;
    23.     }
    24.     public void OnSyncHealthUpdate(int health) {
    25.         LG.log("SYNC OnSyncHealthUpdate :: " + health);
    26.         syncHealth = health;
    27.     }
    28.     public void OnSyncCriticalDamageUpdate(bool criticalDamage) {
    29.         LG.log("SYNC OnSyncCriticalDamageUpdate :: " + criticalDamage);
    30.         syncCriticalDamage = criticalDamage;
    31.     }
    32. }

    At client connect there no hooks and all "players" have wrong sync values at the client.
    At server
    http://prntscr.com/mzbdrn
    http://prntscr.com/mzbdws
    Custom Logs:
    http://prntscr.com/mzbemm

    At client
    http://prntscr.com/mzbe2s
    http://prntscr.com/mzbe54
    Custom Logs:
    http://prntscr.com/mzbeqv
     
  4. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    I can't see anything wrong in your code. Perhaps try without the hooks and see if that works.
     
  5. DrunkGodfather

    DrunkGodfather

    Joined:
    Jan 26, 2018
    Posts:
    3
    This is "magic", because i add hooks to log values to handle it and to see whats wrong. At the end i fix it.
    Some way my Charecter class was to big for syncronizing - there is transforms, animations, triggers, e.t.c. and that stats like speed, health. I create new network class for player prefab that contains only sync vars and it's math\changing logics and it start to work good. I think there was some conflict in sync vars with animations(it's bones based and there many vars on it)
     
  6. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    By default UNET can handle 32 syncvars/lists per NetworkBehaviour. But you should receive errors if you have too many.