Forum begins after the advertisement:


[Part 9] Health bar invisible after restarting game

Home Forums Video Game Tutorial Series Creating a Metroidvania in Unity [Part 9] Health bar invisible after restarting game

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #13979
    Anuk Thotawatta
    Level 5
    Participant
    Helpful?
    Up
    0
    ::

    First after i unlocked a few health slots, and restarted i was back to 5 health so I modified the code to save maxHealth and i now have 10 health but the health is invisible until i get hit. do you know where the problem might be?

    View post on imgur.com

    My save data code:

    <code>using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using System.IO;
    using UnityEngine.SceneManagement;
    using Unity.Mathematics;
    
    [System.Serializable]
    public struct SaveData
    {
        public static SaveData Instance;
    
        //map stuff
        public HashSet<string> sceneNames;
    
        //coffin stuff
        public string coffinSceneName;
        public Vector2 coffinpos;
    
        //player stuff
        public int playerHealth;
        public int playerMaxHealth;
        public int playerHeartShards;
        public float playerMana;
        public int playerManaOrbs;
        public int playerOrbShard;
        public float playerOrb0Fill,playerOrb1Fill,playerOrb2Fill;
        public bool playerHalfMana;
        public Vector2 playerPosition;
        public string lastScene;
    
        //save ability unlocks
        public bool playerUnlockedWallJump;
        public bool playerUnlockedDash;
        public bool playerUnlockedDoubleJump;
        public bool playerUnlockedSideCast;
        public bool playerUnlockedDownCast;
        public bool playerUnlockedUpCast;
    
        //enemy stuff
        //shade
        public Vector2 shadePos;
        public string sceneWithShade;
        public Quaternion shadeRot; 
        public void Initialize(){
            if(!File.Exists(Application.persistentDataPath+"/save.coffin.data")){
                BinaryWriter writer = new BinaryWriter(File.Create(Application.persistentDataPath+"/save.coffin.data"));
            }
            if(!File.Exists(Application.persistentDataPath+"/save.player.data")){
                BinaryWriter writer = new BinaryWriter(File.Create(Application.persistentDataPath+"/save.player.data"));
            }
            if(!File.Exists(Application.persistentDataPath+"/save.shade.data")){
                BinaryWriter writer = new BinaryWriter(File.Create(Application.persistentDataPath+"/save.shade.data"));
            }
            if(sceneNames == null){
                sceneNames = new HashSet<string>();
            }
        }
    
        public void SaveCoffin(){
            using(BinaryWriter writer = new BinaryWriter(File.OpenWrite(Application.persistentDataPath+"/save.coffin.data"))){
                writer.Write(coffinSceneName);
                writer.Write(coffinpos.x);
                writer.Write(coffinpos.y);
            }
        }
    
        public void LoadCoffin(){
            if(File.Exists(Application.persistentDataPath+"/save.coffin.data")){
                using(BinaryReader reader = new BinaryReader(File.OpenRead(Application.persistentDataPath+"/save.coffin.data"))){
                    coffinSceneName = reader.ReadString();
                    coffinpos.x=reader.ReadSingle();
                    coffinpos.y=reader.ReadSingle();
                }
            }
        }
    
        public void SavePlayerData(){
            using(BinaryWriter writer = new BinaryWriter(File.OpenWrite(Application.persistentDataPath+"/save.player.data"))){
                playerHealth = PlayerController.Instance.Health;
                writer.Write(playerHealth);
                playerMaxHealth = PlayerController.Instance.maxHealth;
                writer.Write(playerMaxHealth);
                playerHeartShards = PlayerController.Instance.heartShards;
                writer.Write(playerHeartShards);
    
                playerMana = PlayerController.Instance.Mana;
                writer.Write(playerMana);
                playerHalfMana = PlayerController.Instance.halfMana;
                writer.Write(playerHalfMana);
                playerManaOrbs = PlayerController.Instance.manaOrbs;
                writer.Write(playerManaOrbs);
                playerOrbShard = PlayerController.Instance.orbShard;
                writer.Write(playerOrbShard);
                playerOrb0Fill = PlayerController.Instance.manaOrbsHandler.orbFills[0].fillAmount;
                writer.Write(playerOrb0Fill);
                playerOrb1Fill = PlayerController.Instance.manaOrbsHandler.orbFills[1].fillAmount;
                writer.Write(playerOrb1Fill);
                playerOrb2Fill = PlayerController.Instance.manaOrbsHandler.orbFills[2].fillAmount;
                writer.Write(playerOrb2Fill);
    
    //save ability unlocks
                playerUnlockedWallJump = PlayerController.Instance.unlockedWallJump;
                writer.Write(playerUnlockedWallJump);
                playerUnlockedDash = PlayerController.Instance.unlockedDash;
                writer.Write(playerUnlockedDash);
                playerUnlockedDoubleJump = PlayerController.Instance.unlockedDoubleJump;
                writer.Write(playerUnlockedDoubleJump);
                playerUnlockedSideCast = PlayerController.Instance.unlockedSideCast;
                writer.Write(playerUnlockedSideCast);
                playerUnlockedDownCast = PlayerController.Instance.unlockedDownCast;
                writer.Write(playerUnlockedDownCast);
                playerUnlockedUpCast = PlayerController.Instance.unlockedUpCast;
                writer.Write(playerUnlockedUpCast);
    //end of save ability unlocks
    
                playerPosition = PlayerController.Instance.transform.position;
                writer.Write(playerPosition.x);
                writer.Write(playerPosition.y);
    
                lastScene = SceneManager.GetActiveScene().name;
                writer.Write(lastScene);
            }
        }
    
        public void LoadPlayerData(){
            if(File.Exists(Application.persistentDataPath+"/save.player.data")){
                using(BinaryReader reader = new BinaryReader(File.OpenRead(Application.persistentDataPath+("/save.player.data")))){
                    playerHealth = reader.ReadInt32();
                    playerMaxHealth = reader.ReadInt32();
                    playerHeartShards = reader.ReadInt32();
                    playerMana = reader.ReadSingle();
                    playerHalfMana = reader.ReadBoolean();
                    playerManaOrbs = reader.ReadInt32();
                    playerOrbShard = reader.ReadInt32();
                    playerOrb0Fill = reader.ReadSingle();
                    playerOrb1Fill = reader.ReadSingle();
                    playerOrb2Fill = reader.ReadSingle();
    
    
                    playerUnlockedWallJump = reader.ReadBoolean();
                    playerUnlockedDash = reader.ReadBoolean();
                    playerUnlockedDoubleJump = reader.ReadBoolean();
                    playerUnlockedSideCast = reader.ReadBoolean();
                    playerUnlockedDownCast = reader.ReadBoolean();
                    playerUnlockedUpCast = reader.ReadBoolean();
    
                    playerPosition.x = reader.ReadSingle(); 
                    playerPosition.y = reader.ReadSingle(); 
                    lastScene = reader.ReadString();
    
                    SceneManager.LoadScene(lastScene);
                    PlayerController.Instance.transform.position = playerPosition;
                    PlayerController.Instance.halfMana = playerHalfMana;
                    PlayerController.Instance.health = playerHealth;
                    PlayerController.Instance.maxHealth = playerMaxHealth;
                    PlayerController.Instance.heartShards = playerHeartShards;
                    PlayerController.Instance.Mana = playerMana;
                    PlayerController.Instance.manaOrbs = playerManaOrbs;
                    PlayerController.Instance.orbShard = playerOrbShard;
                    PlayerController.Instance.manaOrbsHandler.orbFills[0].fillAmount = playerOrb0Fill;
                    PlayerController.Instance.manaOrbsHandler.orbFills[1].fillAmount = playerOrb1Fill;
                    PlayerController.Instance.manaOrbsHandler.orbFills[2].fillAmount = playerOrb2Fill;
    
                    PlayerController.Instance.unlockedWallJump = playerUnlockedWallJump;
                    PlayerController.Instance.unlockedDash = playerUnlockedDash;
                    PlayerController.Instance.unlockedDoubleJump = playerUnlockedDoubleJump;
                    PlayerController.Instance.unlockedSideCast = playerUnlockedSideCast;
                    PlayerController.Instance.unlockedDownCast = playerUnlockedDownCast;
                    PlayerController.Instance.unlockedUpCast = playerUnlockedUpCast;
                }
            }
            else{
                Debug.Log("File doesnt exist");
                PlayerController.Instance.halfMana = false;
                PlayerController.Instance.Health = PlayerController.Instance.maxHealth;
                PlayerController.Instance.Mana = 0.5f;
                PlayerController.Instance.heartShards = 0;
    
    
                PlayerController.Instance.unlockedWallJump = false;
    
            }
        }
        public void SaveShadeData(){
            using(BinaryWriter writer = new BinaryWriter(File.OpenWrite(Application.persistentDataPath+"/save.shade.data"))){
                sceneWithShade = SceneManager.GetActiveScene().name;
                shadePos = Shade.Instance.transform.position;
                shadeRot = Shade.Instance.transform.rotation;
    
                writer.Write(sceneWithShade);
                writer.Write(shadePos.x);
                writer.Write(shadePos.y);
    
                writer.Write(shadeRot.y);
                writer.Write(shadeRot.x);
                writer.Write(shadeRot.z);
                writer.Write(shadeRot.w);
            }
        }
    
        public void LoadShadeData(){
            if(File.Exists(Application.persistentDataPath+"/save.shade.data")){
                using(BinaryReader reader = new BinaryReader(File.OpenRead(Application.persistentDataPath+"/save.shade.data"))){
                    sceneWithShade = reader.ReadString();
                    shadePos.x = reader.ReadSingle();
                    shadePos.y = reader.ReadSingle();
    
                    float rotationX = reader.ReadSingle();
                    float rotationY = reader.ReadSingle();
                    float rotationZ = reader.ReadSingle();
                    float rotationW = reader.ReadSingle();
                    shadeRot = new Quaternion(rotationX,rotationY,rotationZ,rotationW);
                }
            }
            else{
                Debug.Log("Shade Doesnt Exist");
            }
        }
    }
    </code>
    #13988
    Joseph Tang
    Level 13
    Moderator
    Helpful?
    Up
    0
    ::

    I don’t believe this is an issue with your SaveData.cs, rather this may just be an issue where the HeartController.cs is taking your Player’s health values before its updated with the SaveData.Instance.LoadPlayerData();.

    You can try calling onHealthChangedCallback.Invoke(); in your PlayerController.cs after the SaveData.Instance.LoadPlayerData(); code, to update the HeartController.cs.

    #13990
    Anuk Thotawatta
    Level 5
    Participant
    Helpful?
    Up
    0
    ::

    Worked first try. thanks alot

Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.

Go to Login Page →


Advertisement below: