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”
- This topic has 17 replies, 3 voices, and was last updated 6 months, 2 weeks ago by Joseph Tang.
-
AuthorPosts
-
June 7, 2024 at 12:40 am #15023::
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 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>public void ResetPlayerData() { playerHealth = PlayerController.Instance.maxHealth; playerMana = 0; lastScene = null; playerUnlockWallJump = false; playerUnlockDash = false; playeUnlockAirJump = false; playerUnlockSideSpell = false; } public void ResetSceneData() { sceneName.Clear(); }
June 7, 2024 at 6:33 pm #15044::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
June 7, 2024 at 6:40 pm #15045::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.
-
AuthorPosts
- You must be logged in to reply to this topic.
Advertisement below: