Search Unity

syncvar not working when hook is applied

Discussion in 'Scripting' started by Resilo, Dec 17, 2017.

  1. Resilo

    Resilo

    Joined:
    Dec 8, 2016
    Posts:
    139
    So i have a syncvar called hp
    when i remove the hook hphook the syncvar is sent from the server to the client correctly
    however as soon as i apply the hook the syncvar is no longer sent from the server to the client

    heres the script im stumped

    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.UI;
    6. using UnityEngine.Networking;
    7.  
    8. public class Stats : NetworkBehaviour
    9. {
    10.     public bool hasbeenattacked;
    11.     public List<GameObject> party = new List<GameObject>();
    12.     public List<GameObject> attackerslist = new List<GameObject>();
    13.     public Transform head;
    14.     [SyncVar]
    15.     public int level;
    16.     [SyncVar(hook = "setname")]
    17.     public string username;
    18.     [SyncVar(hook = "hphook")]
    19.     public float hp;
    20.     public bool dualwield;
    21.     public GameObject weapontwo;
    22.     [SyncVar]
    23.     public float currenthp;
    24.     public float attacklisttimer;
    25.     public float staminacost;
    26.     [SyncVar]
    27.     public float maxhp;
    28.     [SyncVar]
    29.     public float stamina;
    30.     [SyncVar]
    31.     public float currentstamina;
    32.     [SyncVar]
    33.     public float maxstamina;
    34.     public float speed;
    35.     public float currentspeed;
    36.     public float maxspeed;
    37.     public float strength;
    38.     public float currentstrength;
    39.     [SyncVar]
    40.     public string charactername;
    41.     public float maxstrength;
    42.     public GameObject hpbar;
    43.     [SyncVar]
    44.     public float damage;
    45.     public GameObject shield;
    46.     public GameObject weapon;
    47.     public Animator anim;
    48.     public Slider externalhpbar;
    49.     public Slider staminabar;
    50.     public Slider externalstaminabar;
    51.     public bool checkhp;
    52.     public Text nametext;
    53.     public GameObject player;
    54.     public InputField setcharactername;
    55.  
    56.  
    57.     public GameObject externalguistamina;
    58.     public bool growstamina;
    59.     public bool timerstart;
    60.     public float timetillfullstamina;
    61.     public GameObject arrow;
    62.     public float staminagain;
    63.     public float staminagaintime;
    64.     public float circlevalue;
    65.     public Image circlehp;
    66.     public AudioClip shieldblock;
    67.     public AudioClip gethit;
    68.     public AudioClip death;
    69.     public AudioClip selectobject;
    70.     public AudioSource playeraudio;
    71.     public GameObject targeting;
    72.     public GameObject nameblock;
    73.     public GameObject weaponone;
    74.     public GameObject UIuseobject;
    75.     [SyncVar]
    76.     public bool noshield;
    77.     [SyncVar]
    78.     public int xp;
    79.     [SyncVar]
    80.     public int xpgain;
    81.  
    82.     private int originalCount;
    83.     [SyncVar(hook = "setplayerdead")]
    84.     public bool dead;
    85.     public GameObject respawnbutton;
    86.  
    87.  
    88.  
    89.  
    90.     void hphook(float hp)
    91.     {
    92.         currenthp = hp / maxhp;
    93.         externalhpbar.value = Mathf.Lerp(externalhpbar.value, currenthp, 15f);
    94.     }
    95.  
    96.  
    97.  
    98.  
    99.     private void setname(string username)
    100.     {
    101.         nametext.text = username;
    102.     }
    103.  
    104.  
    105.  
    106.  
    107.  
    108.  
    109.  
    110.  
    111.  
    112.  
    113.  
    114.  
    115.  
    116.     [Command]
    117.     public void Cmd_setname(string charactername)
    118.     {
    119.  
    120.         nametext.text = charactername;
    121.         Rpc_setname(charactername);
    122.  
    123.     }
    124.  
    125.  
    126.  
    127.  
    128.     [ClientRpc]
    129.     public void Rpc_setname(string charactername)
    130.     {
    131.  
    132.         nametext.text = charactername;
    133.  
    134.     }
    135.  
    136.  
    137.     void staminafilling()
    138.     {
    139.         if (growstamina == true)
    140.         {
    141.             StartCoroutine(staminafiller());
    142.             if (stamina >= maxstamina)
    143.             {
    144.                 growstamina = false;
    145.                 timerstart = false;
    146.  
    147.             }
    148.         }
    149.  
    150.     }
    151.  
    152.  
    153.  
    154.     void staminanotfull()
    155.     {
    156.         if (stamina != maxstamina && timerstart == false)
    157.         {
    158.  
    159.             StartCoroutine(staminafill());
    160.  
    161.         }
    162.     }
    163.  
    164.  
    165.     IEnumerator staminafiller()
    166.     {
    167.         if (growstamina == true && stamina < maxstamina)
    168.         {
    169.             stamina += staminagain;
    170.             yield return new WaitForSeconds(staminagaintime);
    171.             StartCoroutine(staminafiller());
    172.             if (stamina >= maxstamina)
    173.             {
    174.                 growstamina = false;
    175.                 timerstart = false;
    176.             }
    177.             StopCoroutine(staminafiller());
    178.  
    179.  
    180.         }
    181.  
    182.  
    183.     }
    184.  
    185.  
    186.     IEnumerator staminafill()
    187.     {
    188.         timerstart = true;
    189.         yield return new WaitForSeconds(timetillfullstamina);
    190.         growstamina = true;
    191.  
    192.  
    193.     }
    194.  
    195.  
    196.  
    197.     void strengthdamage()
    198.     {
    199.  
    200.     }
    201.  
    202.  
    203.     void staminacalculate()
    204.     {
    205.         currentstamina = stamina / maxstamina;
    206.  
    207.  
    208.         staminabar.value = Mathf.Lerp(staminabar.value, currentstamina, 15f);
    209.         externalstaminabar.value = Mathf.Lerp(externalstaminabar.value, currentstamina, 15f);
    210.  
    211.     }
    212.  
    213.  
    214.  
    215.     [Command]
    216.     void Cmd_addtoparty(GameObject player)
    217.     {
    218.         party.Add(player);
    219.     }
    220.  
    221.  
    222.     public void addtoattackers(GameObject player)
    223.     {
    224.         attackerslist.Add(player);
    225.         hasbeenattacked = true;
    226.         if (isLocalPlayer)
    227.         {
    228.             Cmd_addtoattackers(player);
    229.             Cmd_attacked(hasbeenattacked);
    230.         }
    231.         if (isServer)
    232.         {
    233.             Rpc_addtoattackers(player);
    234.             Rpc_attacked(hasbeenattacked);
    235.         }
    236.         if (hasbeenattacked == true)
    237.         {
    238.             StartCoroutine(attacklisttime());
    239.             hasbeenattacked = false;
    240.             if (isLocalPlayer)
    241.             {
    242.                 Cmd_attacked(hasbeenattacked);
    243.             }
    244.             if (isServer)
    245.             {
    246.                 Rpc_attacked(hasbeenattacked);
    247.             }
    248.  
    249.  
    250.         }
    251.     }
    252.  
    253.  
    254.  
    255.     public IEnumerator attacklisttime()
    256.     {
    257.         yield return new WaitForSeconds(attacklisttimer * Time.deltaTime);
    258.         if (isLocalPlayer)
    259.         {
    260.             attackerslist.Clear();
    261.         }
    262.  
    263.         if (isServer && transform.tag == "enemy")
    264.         {
    265.             attackerslist.Clear();
    266.         }
    267.     }
    268.  
    269.  
    270.     [Command]
    271.     void Cmd_removefromattackers(GameObject player)
    272.     {
    273.         attackerslist.Remove(player);
    274.         Rpc_removefromattackers(player);
    275.     }
    276.  
    277.     [ClientRpc]
    278.     void Rpc_removefromattackers(GameObject player)
    279.     {
    280.         {
    281.             attackerslist.Remove(player);
    282.         }
    283.     }
    284.  
    285.  
    286.     public void restarttime()
    287.     {
    288.         StopCoroutine(attacklisttime());
    289.         StartCoroutine(attacklisttime());
    290.         if (isLocalPlayer)
    291.         {
    292.             Cmd_restarttime();
    293.         }
    294.         if (isServer)
    295.         {
    296.             Rpc_restarttime();
    297.         }
    298.     }
    299.  
    300.     internal void takingthethedamage(float damage)
    301.     {
    302.         throw new NotImplementedException();
    303.     }
    304.  
    305.     [Command]
    306.     void Cmd_restarttime()
    307.     {
    308.         StopCoroutine(attacklisttime());
    309.         StartCoroutine(attacklisttime());
    310.         Rpc_restarttime();
    311.     }
    312.  
    313.  
    314.     [ClientRpc]
    315.     void Rpc_restarttime()
    316.     {
    317.         StopCoroutine(attacklisttime());
    318.         StartCoroutine(attacklisttime());
    319.  
    320.     }
    321.  
    322.  
    323.     [ClientRpc]
    324.     void Rpc_attacked(bool attacked)
    325.     {
    326.         hasbeenattacked = attacked;
    327.     }
    328.  
    329.     [Command]
    330.     void Cmd_attacked(bool attacked)
    331.     {
    332.         hasbeenattacked = attacked;
    333.         Rpc_attacked(attacked);
    334.     }
    335.  
    336.  
    337.     [ClientRpc]
    338.     void Rpc_addtoattackers(GameObject player)
    339.     {
    340.  
    341.         attackerslist.Add(player);
    342.  
    343.     }
    344.  
    345.     [Command]
    346.     void Cmd_addtoattackers(GameObject player)
    347.     {
    348.         attackerslist.Add(player);
    349.         Rpc_addtoattackers(player);
    350.     }
    351.  
    352.  
    353.  
    354.     void setcircle()
    355.     {
    356.         circlevalue = 1 - currenthp;
    357.         circlehp.CrossFadeAlpha(circlevalue, 0.5f, true);
    358.     }
    359.  
    360.  
    361.  
    362.     public void setplayerdead(bool dead)
    363.         {
    364.         anim.SetBool("death", dead);
    365.         anim.SetBool("lay", dead);
    366.   GetComponent<combatmovement>().enabled = !dead;
    367.     GetComponent<Regularmovement>().enabled = !dead;
    368. GetComponent<Attackdirection>().enabled = !dead;
    369.         if (isLocalPlayer)
    370.         {
    371.             respawnbutton.SetActive(dead);
    372.         }
    373.     }
    374.  
    375.  
    376.     public override void OnStartClient()
    377.     {
    378.         base.OnStartClient();
    379.         setname(username);
    380.         if (dead == true && !isServer)
    381.         {
    382.             anim.SetBool("death", dead);
    383.             anim.SetBool("lay", dead);
    384.             GetComponent<combatmovement>().enabled = !dead;
    385.             GetComponent<Regularmovement>().enabled = !dead;
    386.             GetComponent<Attackdirection>().enabled = !dead;
    387.  
    388.         }
    389.     }
    390.  
    391.  
    392.  
    393.     // Use this for initialization
    394.     void Start()
    395.     {
    396.  
    397.         if (GetComponent<Charactersize>() != null)
    398.         {
    399.             if (GetComponent<Charactersize>().weight > 200 && transform.tag != "player")
    400.             {
    401.                 GetComponent<AItargeting>().playertoofar *= 4;
    402.             }
    403.         }
    404.         if (transform.tag == "player")
    405.         {
    406.             nametext.text = username;
    407.  
    408.             if (isLocalPlayer)
    409.             {
    410.                 Cmd_addtoparty(gameObject);
    411.             }
    412.             circlehp.canvasRenderer.SetAlpha(0.0f);
    413.         }
    414.         if (isLocalPlayer)
    415.         {
    416.             hpbar.SetActive(false);
    417.             externalguistamina.SetActive(false);
    418.             nameblock.SetActive(false);
    419.         }
    420.  
    421.         if (!isLocalPlayer)
    422.         {
    423.             if (transform.tag == "player")
    424.             {
    425.  
    426.             }
    427.  
    428.         }
    429.     }
    430.  
    431.  
    432.  
    433.  
    434.  
    435.     // Update is called once per frame
    436.     void Update()
    437.     {
    438.  
    439.  
    440.         if(anim == null)
    441.         {
    442.             anim = GetComponent<Animator>();
    443.         }
    444.  
    445.         if (isServer)
    446.         {
    447.             staminanotfull();
    448.             staminafilling();
    449.         }
    450.         if(!isLocalPlayer)
    451.         {
    452.             return;
    453.         }
    454.         setcircle();
    455.     }
    456.  
    457. }
     
  2. tonemcbride

    tonemcbride

    Joined:
    Sep 7, 2010
    Posts:
    1,089
    If you use a hook then the variable isn't synced automatically anymore - you need to do it yourself.

    e.g.

    Code (CSharp):
    1.  private void setname(string a_username)
    2. {
    3.    username = a_username;
    4.    nametext.text = a_username;
    5. }