Forum begins after the advertisement:


[part 7] 9 minute error wont go

Home Forums Video Game Tutorial Series Creating a Metroidvania in Unity [part 7] 9 minute error wont go

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #12708
    Niyam Shah
    Level 8
    Participant
    Helpful?
    Up
    0
    ::

    at 9 minutes in the video shows an error – it says to save your game in the bench and restart to fix it – ive tried this for 10 minuted and the error persists. My error is identical to the videos so not sure if he did anything different – i even restarted unity to see if it worked?

    #12709
    Niyam Shah
    Level 8
    Participant
    Helpful?
    Up
    0
    ::

    This feature worked for all the other save data bits – even when i remove the wall jumping bits from the save data script, the console will throw the same error – not sure what happened

    #12710
    Niyam Shah
    Level 8
    Participant
    Helpful?
    Up
    0
    ::

    The DebugLog – “worked” doesnt play – neither does the “file doesnt exist”

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using System.IO;
    using UnityEngine.SceneManagement;
    
    [System.Serializable]
    public struct SaveData
    {
        public static SaveData Instance;
    
        //maps
        public HashSet<string> sceneNames;
    
        //savePoints
        public string savePointSceneName;
        public Vector2 savePointPos;
    
        //player
        public int playerHealth;
        public float playerMana;
        public Vector2 playerPosition;
        public string lastScene;
        public bool playerUnlockedWallJump;
    
    
    
        public void Initialize()
        {
            if(!File.Exists(Application.persistentDataPath + "/save.savePoint.data"))
            {
                BinaryWriter writer = new BinaryWriter(File.Create(Application.persistentDataPath + "/save.savePoint.data"));
            }
            if (!File.Exists(Application.persistentDataPath + "/save.player.data"))
            {
                BinaryWriter writer = new BinaryWriter(File.Create(Application.persistentDataPath + "/save.player.data"));
            }
            if (sceneNames == null)
            {
                sceneNames = new HashSet<string>();
            }
        }
    
        public void SaveSavePoint()
        {
            using (BinaryWriter writer = new BinaryWriter(File.OpenWrite(Application.persistentDataPath + "/save.savePoint.data")))
            {
                writer.Write(savePointSceneName);
                writer.Write(savePointPos.x);
                writer.Write(savePointPos.y);  
            }
        }
    
        public void LoadSavePoint()
        {
            if (!File.Exists(Application.persistentDataPath + "/save.savePoint.data"))
            {
                using(BinaryReader reader = new BinaryReader(File.OpenRead(Application.persistentDataPath + "/save.savePoint.data")))
                {
                    savePointSceneName = reader.ReadString();
                    savePointPos.x = reader.ReadSingle();
                    savePointPos.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);
    
                playerMana = PlayerController.Instance.Mana;
                writer.Write(playerMana);
    
                playerUnlockedWallJump = PlayerController.Instance.unlockWallJump;
                writer.Write(playerUnlockedWallJump);
    
                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();
                    playerMana = reader.ReadSingle();
                    playerPosition.x = reader.ReadSingle();
                    playerPosition.y = reader.ReadSingle();
                    lastScene = reader.ReadString();
                    playerUnlockedWallJump = reader.ReadBoolean();
    
                    SceneManager.LoadScene(lastScene);
                    PlayerController.Instance.transform.position = playerPosition;
                    PlayerController.Instance.Health = playerHealth;
                    PlayerController.Instance.Mana = playerMana;
                    PlayerController.Instance.unlockWallJump = playerUnlockedWallJump;
                    Debug.Log("worked");
                }
            }
            else
            {
                Debug.Log("file doesnt exist");
                PlayerController.Instance.Health = PlayerController.Instance.maxHealth;
                PlayerController.Instance.Mana = 0.5f;
                PlayerController.Instance.unlockWallJump = false;
            }
        }
    }
    #12713
    Terence
    Level 30
    Keymaster
    Helpful?
    Up
    0
    ::

    Only thing I can tell you here is that if the Debug.Log doesn’t play, it means that LoadPlayerData() is not firing at all.

    #12716
    Niyam Shah
    Level 8
    Participant
    Helpful?
    Up
    0
    ::

    ah yes i figured this out yesterday – it was a formatting issue:

    had to keep my writer and readers for transforming player position and loading scene at the bottom – not before anything else

    #12723
    Terence
    Level 30
    Keymaster
    Helpful?
    Up
    0
    ::

    Thanks for sharing your solution Niyam!

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

Go to Login Page →


Advertisement below: