Search Unity

SyncVar strange behaviour on LocalPlayerAuthority object

Discussion in 'Multiplayer' started by ttse, Jun 15, 2015.

  1. ttse

    ttse

    Joined:
    Feb 25, 2015
    Posts:
    4
    I have a NetworkBehaviour script which contains a SyncVar int. If I check LocalPlayerAuthority on the GameObject, the SyncVar int's value become "out of control" on the local player.

    By "out of control", this is what I observed in the debug log:
    -int is initialized and sync-ed correctly,;
    -an instant later hook function gets called on client for about 40 times attempting to change the value to 66, 63 and 65 in a random manner
    -sometimes the int is hooked to set consecutively for the same value like 65 for 5 times in a row.
    -after a while hook function gets called to set to a larger variety of numbers
    -few seconds later since above, hook function stops getting called and the int stopped at the value 179

    The only place where I touch this int is OnStartServer() where I set it to 100, and the hook function setting the int to the argument of the hook.

    Is this a bug? but looks pretty crazy to be a bug and from what I read about how SyncVar work this seem too strange also no one else seem to be having problem with SyncVar so I'm not sure if I have totally misunderstood something. Any insight is appreciated, thank you.
     
  2. Necromantic

    Necromantic

    Joined:
    Feb 11, 2013
    Posts:
    116
    I use [SyncVar] with floats on LocalPlayerAuthority objects and not experiencing this. Do you have some kind of feedback loop somewhere?
     
  3. ttse

    ttse

    Joined:
    Feb 25, 2015
    Posts:
    4
    Just attached another test script to the LocalPlayerAuthority object:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Networking;
    3. using System.Collections;
    4.  
    5. public class test : NetworkBehaviour {
    6.  
    7.   [SyncVar(hook="OnUpdate")]
    8.   public int TestValue;
    9.  
    10.    public override void OnStartServer () {
    11.   base.OnStartServer();
    12.   TestValue = 2;
    13.    }
    14.  
    15.   public override void OnStartLocalPlayer()
    16.   {
    17.   base.OnStartLocalPlayer();
    18.   TestValue = 3;
    19.   }
    20.  
    21.    void OnUpdate (int newValue) {
    22.   Debug.Log("Oldvalue: " + TestValue);
    23.   TestValue = newValue;
    24.   Debug.Log("Newvalue: " + newValue);
    25.    }
    26. }
    Nowhere else touches TestValue, in the log I get:
    Oldvalue: 3
    Newvalue: 2
    Oldvalue: 2
    Newvalue: 1777
    Oldvalue: 1777
    Newvalue: 185
    Oldvalue: 185
    Newvalue: 230
    Oldvalue: 230
    Newvalue: -734295235
    Oldvalue: -734295235
    Newvalue: 235
    Oldvalue: 235
    Newvalue: 36
    Oldvalue: 36
    Newvalue: 902839807

    no more logs afterwards

    Do SyncVar work when the GameObject has multiple NetworkBehaviour components attached ( though I don't recall reading anywhere mentioning it)?

    Basically I have 3 scripts on my object which I separate the GameObject's function into. I have one script which poll player input, one script which give instruction based on the input result and one script (where all SyncVar are supposed to live in) which store game data like current score for the player. I suspect the system do not like my setup....
     
    Last edited: Jun 15, 2015
  4. Necromantic

    Necromantic

    Joined:
    Feb 11, 2013
    Posts:
    116
    Those value changes seem pretty weird and random.
    Mine do have multiple NetworkBehaviours, but I'm not using the default callbacks. I have one that does basic logic, one that handles controlling and one that changes appearance properties.
    I use the Client Message Handlers and RPCs/Commands.
     
    Last edited: Jun 15, 2015
  5. ttse

    ttse

    Joined:
    Feb 25, 2015
    Posts:
    4
    Thanks so multiple NetworkBehaviours do work.

    Well in the original script I referred to on topic I only used Start() and regardless the weird behaviour still happened so I don't think the problem is due to these callbacks. I guess I will file in a bug with a clean project...
     
  6. Necromantic

    Necromantic

    Joined:
    Feb 11, 2013
    Posts:
    116
  7. ttse

    ttse

    Joined:
    Feb 25, 2015
    Posts:
    4
    Didn't know such thing exists lol, thanks man I will give it a try!

    Edit: Yes the patched version solved the problem concerned.
     
    Last edited: Jun 18, 2015