Forum begins after the advertisement:
[Part 7] My Maps doesn’t update when I quit the game and enter again
Home › Forums › Video Game Tutorial Series › Creating a Metroidvania in Unity › [Part 7] My Maps doesn’t update when I quit the game and enter again
- This topic has 10 replies, 2 voices, and was last updated 6 months, 3 weeks ago by Elvin Sim.
-
AuthorPosts
-
May 21, 2024 at 6:34 pm #14737May 21, 2024 at 6:35 pm #14738May 21, 2024 at 6:36 pm #14739::
<code>using System.Collections; using System.Collections.Generic; using UnityEngine; using System.IO; using UnityEngine.SceneManagement; [System.Serializable] public struct SaveData { public static SaveData Instance; //map stuff public HashSet<string> sceneNames; //bench stuff public string benchSceneName; public Vector2 benchPos; //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; public bool playerUnlockedWallJump, playerUnlockedDash, playerUnlockedVarJump; public bool playerUnlockedSideCast, playerUnlockedUpCast, playerUnlockedDownCast; //enemies stuff //shade public Vector2 shadePos; public string sceneWithShade; public Quaternion shadeRot; public void Initialize() { if(!File.Exists(Application.persistentDataPath + "/save.bench.data")) { BinaryWriter writer = new BinaryWriter(File.Create(Application.persistentDataPath + "/save.bench.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>(); } } #region Bench Stuff public void SaveBench() { using (BinaryWriter writer = new BinaryWriter(File.OpenWrite(Application.persistentDataPath + "/save.bench.data"))) { writer.Write(benchSceneName); writer.Write(benchPos.x); writer.Write(benchPos.y); } } public void LoadBench() { string savePath = Application.persistentDataPath + "/save.bench.data"; if (File.Exists(savePath) && new FileInfo(savePath).Length > 0) { using(BinaryReader reader = new BinaryReader(File.OpenRead(Application.persistentDataPath + "/save.bench.data"))) { benchSceneName = reader.ReadString(); benchPos.x = reader.ReadSingle(); benchPos.y = reader.ReadSingle(); } } else { Debug.Log("Bench doesn't exist"); } } #endregion #region Player Stuff 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); playerUnlockedWallJump = PlayerController.Instance.unlockedWallJump; writer.Write(playerUnlockedWallJump); playerUnlockedDash = PlayerController.Instance.unlockedDash; writer.Write(playerUnlockedDash); playerUnlockedVarJump = PlayerController.Instance.unlockedVarJump; writer.Write(playerUnlockedVarJump); playerUnlockedSideCast = PlayerController.Instance.unlockedSideCast; writer.Write(playerUnlockedSideCast); playerUnlockedUpCast = PlayerController.Instance.unlockedUpCast; writer.Write(playerUnlockedUpCast); playerUnlockedDownCast = PlayerController.Instance.unlockedDownCast; writer.Write(playerUnlockedDownCast); playerPosition = PlayerController.Instance.transform.position; writer.Write(playerPosition.x); writer.Write(playerPosition.y); lastScene = SceneManager.GetActiveScene().name; writer.Write(lastScene); } Debug.Log("saved player data"); } public void LoadPlayerData() { string savePath = Application.persistentDataPath + "/save.player.data"; if (File.Exists(savePath) && new FileInfo(savePath).Length > 0) { 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(); playerUnlockedVarJump= reader.ReadBoolean(); playerUnlockedSideCast= reader.ReadBoolean(); playerUnlockedUpCast = reader.ReadBoolean(); playerUnlockedDownCast = 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.unlockedVarJump = playerUnlockedVarJump; PlayerController.Instance.unlockedSideCast = playerUnlockedSideCast; PlayerController.Instance.unlockedUpCast = playerUnlockedUpCast; PlayerController.Instance.unlockedDownCast = playerUnlockedDownCast; } Debug.Log("load player data"); Debug.Log(playerHalfMana); } else { Debug.Log("File doesn't exist"); PlayerController.Instance.halfMana = false; PlayerController.Instance.Health = PlayerController.Instance.maxHealth; PlayerController.Instance.Mana = 0.5f; PlayerController.Instance.heartShards = 0; PlayerController.Instance.unlockedWallJump = false; PlayerController.Instance.unlockedDash = false; PlayerController.Instance.unlockedVarJump = false; PlayerController.Instance.unlockedSideCast = false; PlayerController.Instance.unlockedUpCast = false; PlayerController.Instance.unlockedDownCast = false; } } #endregion #region Shade Stuff 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.x); writer.Write(shadeRot.y); writer.Write(shadeRot.z); writer.Write(shadeRot.w); } } public void LoadShadeData() { string savePath = Application.persistentDataPath + "/save.shade.data"; if (File.Exists(savePath) && new FileInfo(savePath).Length > 0) { 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); } Debug.Log("Load shade data"); } else { Debug.Log("Shade doesn't exist"); } } #endregion }</code>
May 21, 2024 at 6:36 pm #14740::<code>using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class MapManager : MonoBehaviour { [SerializeField] GameObject[] maps; Bench bench; private void OnEnable() { bench = FindObjectOfType<Bench>(); if ( bench != null ) { if(bench.interacted ) { UpdateMap(); } } } void UpdateMap() { var savedScenes = SaveData.Instance.sceneNames; for(int i = 0; i < maps.Length; i++) { if(savedScenes.Contains("The Forest of Beginnings_" + (i + 1))) { maps[i].SetActive(true); } else if (savedScenes.Contains("The Ruins of Unity_" + (i + 1))) { maps[i].SetActive(true); } else if (savedScenes.Contains("The Bastion of Strife_" + (i + 1))) { maps[i].SetActive(true); } else if (savedScenes.Contains("The Abyssal Depths_" + (i + 1))) { maps[i].SetActive(true); } else { maps[i].SetActive(false); } } } }</code>
May 21, 2024 at 6:37 pm #14741::<code>using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; public class Bench : MonoBehaviour { bool inRange = false; public bool interacted; // Update is called once per frame void Update() { if (Input.GetButtonDown("Interact") && inRange) { interacted = true; SaveData.Instance.benchSceneName = SceneManager.GetActiveScene().name; SaveData.Instance.benchPos = new Vector2(gameObject.transform.position.x, gameObject.transform.position.y); SaveData.Instance.SaveBench(); SaveData.Instance.SavePlayerData(); Debug.Log("benched"); } } private void OnTriggerEnter2D(Collider2D _collision) { if(_collision.CompareTag("Player")) inRange = true; } private void OnTriggerExit2D(Collider2D _collision) { if (_collision.CompareTag("Player")) { interacted = false; inRange = false; } } }</code>
May 22, 2024 at 3:45 pm #14748::Try putting
UpdateMap()
method into your PlayerController.csStart()
orAwake()
method. Likely, the maps are saved, but not called in to update anywhere except for the benchesMay 29, 2024 at 11:19 pm #14802May 29, 2024 at 11:36 pm #14803May 29, 2024 at 11:49 pm #14805May 30, 2024 at 12:34 am #14806::In your PlayerController.cs
// Start is called before the first frame update void Start() { ... audioSource = GetComponent<AudioSource>(); SaveData.Instance.LoadPlayerData(); FindObjectOfType[MapManager]().UpdateMap(); if(manaOrbs > 3) { manaOrbs = 3; } ... }
Just change out the “[]” with angle brackets “><“.
Note that the biggest issue with this is that your maps will now update on the start of the game instead of only when you sit on the bench. to prevent map updating on start and still get your last saved maps, you’ll need to add additional code to your SaveData.cs
Essentially, you’ll need to create the same code for saving scene names for maps, but only saving them whenever you sit down on a bench, then loading that information through the MapManager and then the PlayerController instead of this one.
May 30, 2024 at 2:35 am #14809 -
AuthorPosts
- You must be logged in to reply to this topic.
Advertisement below: