Health sliders only updates in the master client



  • Hi all,
    I'm facing that issue where the UI Sliders only gets updated in the Master Client, another joined player doesn't see his health bar getting rendered or updated at all, and also doesn't see the other players bars getting updated.

    Also on respawn, the UI health bar doesn't reset its values.
    Unity version is 2018.3

    alt text

    The refactors i did to the codebase was port to PUN2. and make the player data (health,moveSpeed, fire rate) comes from ScriptableObject or Json

    Here is also the Player.cs class and my added method to PlayerExtensions.cs

            /// <summary>
            /// The Data of the player
            /// MaxHealth, MoveSpeed,FireRate will be thrown here
            /// </summary>
            public CharacterConf PlayerData;
    
            /// <summary>
            /// Maximum health value at game start.
            /// </summary>
            public int maxHealth = 12;
    
            /// <summary>
            /// UI Slider visualizing health value.
            /// </summary>
            public Slider healthSlider;
    
            //initialize server values for this player
            void Awake()
            {
                //only let the master do initialization
                if(!PhotonNetwork.IsMasterClient)
                    return;
    
                InitData();
                //set players current health value after joining
                GetView().SetHealth(maxHealth);
            }
    
    
            /// <summary>
            /// Initialize synced values on every client.
            /// Initialize camera and input for this local client.
            /// </summary>
            void Start()
            {           
                //call hooks manually to update
                OnHealthChange(GetView().GetHealth());
                OnShieldChange(GetView().GetShield());
    
               [...]
            }
            
            
            /// <summary>
            /// This method gets called whenever player properties have been changed on the network.
            /// </summary>
            public override void OnPlayerPropertiesUpdate(Photon.Realtime.Player player, Hashtable table)
            {
                if(player != photonView.Owner)
                    return;
    
                //update values that could change any time for visualization to stay up to date
                OnHealthChange(player.GetHealth());
                OnShieldChange(player.GetShield());
            }
    
    
            //hook for updating health locally
            //(the actual value updates via player properties)
            protected void OnHealthChange(int value)
            {
                healthSlider.value = (float)value / maxHealth;
            }
    
    
            /// <summary>
            /// Server only: calculate damage to be taken by the Player,
    		/// triggers score increase and respawn workflow on death.
            /// </summary>
            public void TakeDamage(Bullet bullet)
            {
                //store network variables temporary
                int health = GetView().GetHealth();
                int shield = GetView().GetShield();
    
                //substract health by damage
                //locally for now, to only have one update later on
                health -= bullet.damage;
    
                //bullet killed the player
                if (health <= 0)
                {
                    [...]
    
                    //the game is not over yet, reset runtime values
                    //also tell all clients to despawn this player
                    GetView().SetHealth(maxHealth);
                    GetView().SetBullet(0);
    
                    [...]
                }
                else
                {
                    //we didn't die, set health to new value
                    GetView().SetHealth(health);
                }
            }
    
            #region Data Init
            private void InitData()
            {
                string playerId = PlayerExtensions.GetCharacter(photonView.Owner);
                Debug.Log("Player ID is: " + playerId);
                PlayerData = ConfManager.Instance.CharactersData.GetCharacterConfById(playerId);
                if(PlayerData != null)
                {
                    maxHealth = PlayerData.MaxHealth;
                    moveSpeed = PlayerData.MoveSpeed;
                    fireRate = PlayerData.FireRate;
                } else
                {
                    Debug.LogError(string.Format("Cannot find Conf file for player {0}", playerId));
                }
                
            }
            #endregion
    

    Has anybody faced that UI rendering issue ?



  • Solved:
    that was pretty dumb actually, i was setting the tank data only on the master.
    i moved InitData() a bit up.



  • Good to hear you've solved the issue ;)

    Please avoid posting full scripts though; the parts related to "Health" would be sufficient in this case. Full scripts make it difficult to focus on the actual problem, and as this is a semi-private forum, other members have access to the full script as well.

    PUN2 conversion is also planned for this year, along with more features if time permits!


Log in to reply