Forum begins after the advertisement:


[Part 7] How can I let the game have “new game”

Home Forums Video Game Tutorial Series Creating a Metroidvania in Unity [Part 7] How can I let the game have “new game”

Viewing 3 posts - 16 through 18 (of 18 total)
  • Author
    Posts
  • #15023
    Joseph Tang
    Level 13
    Moderator
    Helpful?
    Up
    0
    ::

    Okay so let’s do 2 things.

    1. Stop your player from respawning everytime you start the game.

    believe the issue is that your Player Prefab’s inspector value for their health is just set to 0. This causes the player to always spawn in with 0 health and call respawn no matter what. Let’s fix this by first.

    Change your Player’s Health value in their inspector to [100].

    Rearranging your code.

        void Start()
        {
            pState = GetComponent<PlayerStateList>();
    
            rb = GetComponent<Rigidbody2D>();
    
            sr = GetComponent<SpriteRenderer>();
    
            anim = GetComponent<Animator>();
    
            gravity = rb.gravityScale;
    
            if(Health == 0)
            {
                pState.alive = false;
                GameManager.Instance.RespawnPlayer();
            }
    
            //Health = maxHealth; Save之後不再需要
            //Mana = maxMana;
    
            healthImage.fillAmount = health / maxHealth;
            healthEffect.fillAmount = health / maxHealth;
            healthText.text = health.ToString() + "/" + maxHealth.ToString();
            manaImage.fillAmount = Mana;
    
            SaveData.Instance.LoadPlayerData();
    
            if(Health == 0)
            {
                pState.alive = false;
                GameManager.Instance.RespawnPlayer();
            }
        }

    This way, your player’s mana and health will be set to full initially, but will then be set to your Player’s saved data information if there is any, and then will finally be checked if the player is dead on start.


    2. Resetting your data completely with an updated code.

    The issue was that the variables of the SaveData.cs was still retaining it’s value somehow. So let’s ensure that we can completely reset the data by also resetting the variables instead of just the file path.

    SaveData.cs

        public void ResetSavePoint()
        {
            savePointSceneName = null;
            savePointPos = Vector2.zero;
        }
    
    public void ResetPlayerData()
    {
        playerHealth = PlayerController.Instance.maxHealth;
        playerMana = 0;
        lastScene = null;
        playerUnlockWallJump = false;
        playerUnlockDash = false;
        playeUnlockAirJump = false;
        playerUnlockSideSpell = false;
    }
    
    public void ResetSceneData()
    {
        sceneName.Clear();
    }
    public void DeleteScenesData() { string path = Application.persistentDataPath + "/save.scenes.data"; if (File.Exists(path)) { File.Delete(path); Debug.Log("Scenes data deleted."); } ResetSceneData(); } public void DeletePlayerData() { string path = Application.persistentDataPath + "/save.player.data"; if (File.Exists(path)) { File.Delete(path); Debug.Log("Player save data deleted."); } ResetPlayerData(); } public void DeleteSavePointData() { string path = Application.persistentDataPath + "/save.savePoint.data"; if (File.Exists(path)) { File.Delete(path); Debug.Log("Save point data deleted."); } ResetSavePoint(); } public void DeleteAllSaveData() { DeleteScenesData(); DeleteSavePointData(); DeletePlayerData(); }<pre>
    #15044
    MI NI
    Level 14
    Bronze Supporter (Patron)
    Helpful?
    Up
    0
    ::

    Thank you, it worked successfully. I used the first method and it worked successfully. As for the second method, I only create and delete data when I am in MainMenu, so I am not sure whether I should keep this part in SaveData.cs. Code to delete data. The deleted data in SaveData.cs is used by Input.GetKey

    #15045
    Joseph Tang
    Level 13
    Moderator
    Helpful?
    Up
    0
    ::

    I’d recommend you simply instantiate the SaveData.cs in the MenuSaveData.cs instead to make this work.

    MenuSaveData.cs

        public void ResetSaveData()
        {
            ...
    
            SaveData.Instance.Initialize();
    
            //Either
            SaveData.Instance.DeletAllSaveData();
    
            //Or
            SaveData.Instance.sceneName.Clear();
    
            Debug.Log("All save data has been reset.");
        }

    Just instantiate SaveData.cs so that you can access its variables to delete directly or call the deletion methods.

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

Go to Login Page →


Advertisement below: