Forum begins after the advertisement:


[Part 7] Player not loading at my save point

Home Forums Video Game Tutorial Series Creating a Metroidvania in Unity [Part 7] Player not loading at my save point

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #13839
    Aria
    Bronze Supporter (Patron)
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using System.IO;
    using UnityEngine.SceneManagement;
    
    [System.Serializable]
    public struct SaveData
    {
        public static SaveData Instance;
        public HashSet<string> sceneNames;
        public string typewriterSceneName;
        public Vector2 typewriterPos;
        public int playerHealth;
        public Vector2 playerPos;
        public string lastScene;
        public string lastFrame;
        public bool playerUnlockedDash;
        public bool playerUnlockedDoubleJump;
    
        public void Initialized()
        {
            if(!File.Exists(Application.persistentDataPath + "/save.typewriter.data"))
            {
                BinaryWriter writer = new BinaryWriter(File.Create(Application.persistentDataPath + "/save.typewriter.data"));
            }
            if (!File.Exists(Application.persistentDataPath + "/save.player.data")) //if file doesnt exist, well create the file
            {
                BinaryWriter writer = new BinaryWriter(File.Create(Application.persistentDataPath + "/save.player.data"));
            }
    
            if (sceneNames == null)
            {
                sceneNames = new HashSet<string>(); 
    
            }
        }
       public void SaveTypewriter()
        {
            using (BinaryWriter writer = new BinaryWriter(File.OpenWrite(Application.persistentDataPath + "save.typewriter.data")))
            {
                writer.Write(typewriterSceneName);
                writer.Write(typewriterPos.x);
                writer.Write(typewriterPos.y);
            }
            
        }
        public void LoadTypewriter()
        {
            if(File.Exists(Application.persistentDataPath + "save.typewriter.data"))
            {
                using (BinaryReader reader = new BinaryReader(File.OpenRead(Application.persistentDataPath + "save.typewriter.data")))
                {
                    typewriterSceneName = reader.ReadString();
                    typewriterPos.x = reader.ReadSingle();  
                    typewriterPos.y = reader.ReadSingle();
                }
            }
           
        }
        public void SavePlayerData()
        {
    
            using (BinaryWriter writer = new BinaryWriter(File.OpenWrite(Application.persistentDataPath + "save.player.data")))
            {
                playerHealth = PlayerMovement.Instance.Health;
                writer.Write(playerHealth);
                playerPos = PlayerMovement.Instance.transform.position;
                writer.Write(playerPos.x);
                writer.Write(playerPos.y);
                playerUnlockedDash = PlayerMovement.Instance.unlockedDash;
                writer.Write(playerUnlockedDash);
                playerUnlockedDoubleJump = PlayerMovement.Instance.unlockedDoubleJump;
                writer.Write(playerUnlockedDoubleJump);
    
                lastScene = SceneManager.GetActiveScene().name;
                writer.Write(lastScene);
            }
            Debug.Log("saved player data");
            
    
        }
        public void LoadPlayerData()
        {
            if (File.Exists(Application.persistentDataPath + "save.player.data"))
            {
                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();
                        playerUnlockedDash = reader.ReadBoolean();
                        playerUnlockedDoubleJump = reader.ReadBoolean();
                        
                        playerPos.x = reader.ReadSingle();
                        playerPos.y = reader.ReadSingle();
                        lastScene = reader.ReadString();
    
                        SceneManager.LoadScene(lastScene);
                        PlayerMovement.Instance.transform.position = playerPos;
                        PlayerMovement.Instance.Health = playerHealth;
                        PlayerMovement.Instance.unlockedDash = playerUnlockedDash;
                        PlayerMovement.Instance.unlockedDoubleJump = playerUnlockedDoubleJump;
    
                    }
                    Debug.Log("load player data");
                   
    
                }
    
            }
            else
            {
                Debug.Log("File doesnt exist");
                
                PlayerMovement.Instance.health = PlayerMovement.Instance.maxHealth;
                PlayerMovement.Instance.unlockedDash = false;
                PlayerMovement.Instance.unlockedDoubleJump = false;
                
            }
            
        }
    
    }

    This is how my SaveData is set up, My player controller is named PlayerMovement, my “bench” is a typewriter

    null

    null

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.SceneManagement;
    
    public class TypewriterCheck : MonoBehaviour
    {
        public bool interacted;
        // Start is called before the first frame update
        void Start()
        {
            
        }
    
        // Update is called once per frame
        void Update()
        {
            
        }
        private void OnTriggerStay2D(Collider2D _collision)
        {
            if(_collision.CompareTag("Player") && Input.GetButtonDown("Interact"))
            {
                interacted = true;
                SaveData.Instance.typewriterSceneName = SceneManager.GetActiveScene().name;
                SaveData.Instance.typewriterPos = new Vector2(gameObject.transform.position.x, gameObject.transform.position.y);
                SaveData.Instance.SaveTypewriter();
                SaveData.Instance.SavePlayerData();
            }
           
        }
        void OnTriggerExit2D(Collider2D _collision)
        {
            if (_collision.CompareTag("Player"))
            {
                interacted = false;
            }
        }
    
    }

    My player when i load the game is not loading in at the typewriter position

    #13840
    Joseph Tang
    Moderator

    So far, I cannot see any issues with your code.
    I have also tested it out and my end and can see no problem with the code yet.

    To identify your issue, could you show me a video or screenshot of:
    1) Your Player dying and respawning [In the same scene as the typewriter and a different scene.]
    2) Your Scene’s Console & Game Manager object [Can be found in the Dont Destroy On Load section of the scene]
    3) Your GameManager.cs

    I’m unsure if it’s possible to share a video via discord images as you did earlier, but if it doesn’t work you can send the video via Imgur link

    #13849
    Aria
    Bronze Supporter (Patron)

    Weirdly enough I opened unity 2 days ago and since then it has worked perfectly with no errors whatsoever

    #13854
    Joseph Tang
    Moderator

    That’s great to hear that it’s fine.

    If you ever encounter the same issue again, do tell us and send some more videos or screenshots so that we may address it for you if it happens again.

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

Go to Login Page →


Advertisement below: