Forum begins after the advertisement:


[Request] New Game

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #13677
    William Tyrant
    Participant

    Not a bug but just a question. Is there a way that we can add an option in the main menu that will start a fresh new save game and delete the current game profile.
    And also after we quit the game and open it again, how can i make the player repawn at the bench, not the last position when i quit. Im new to unity so please help

    #13692
    Joseph Tang
    Moderator

    I will have to get back to you on the Reset data button.

    As for reloading the game to only the Bench position I can help. I’m assuming this means pausing the game then exiting to main menu and quitting, rather than closing the game mid playthrough. Also, I’m assuming you have completed the scripts until Part 9.

    In your SaveData.cs, try out this:

        public void SavePlayerData()
        {
            using (BinaryWriter writer = new BinaryWriter(File.OpenWrite(Application.persistentDataPath + "/save.player.data")))
            {
                playerHealth = PlayerController.Instance.Health;
                writer.Write(playerHealth);
                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);
            }
        }
    
        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();
                    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();
    
                    benchPos.x = reader.ReadSingle();
                    benchPos.y = reader.ReadSingle();
                    benchSceneName = reader.ReadString();
    
                    SceneManager.LoadScene(lastScene);
                    PlayerController.Instance.transform.position = playerPosition;
    
                    SceneManager.LoadScene(benchSceneName);
                    PlayerController.Instance.transform.position = benchPos;
                    PlayerController.Instance.halfMana = playerHalfMana;
                    PlayerController.Instance.Health = playerHealth;
                    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;
                }
            }

    Under SavePlayerData() you can comment out/remove the playerPosition & lastScene codes as you won’t need it for this purpose. While in your LoadPlayerDate() replace playerPosition codes with benchPos & lastScene codes to benchSceneName. Doing this will allow your player to load into the game with the correct health, mana and etc, while loading at the Bench position and scene you last saved in.

    However, you will also need a way to save the data as the only codes saving it now is the unlocks, the shade and death.

    In your SceneFader, try this:

    public void CallFadeAndLoadScene(string _sceneToLoad)
        {
            SaveData.Instance.SavePlayerData();
            StartCoroutine(FadeAndLoadScene(FadeDirection.In, _sceneToLoad));
        }

    Under CallFadeAndLoadScene(), add SaveData.Instance.SavePlayerData(); before starting the coroutine. This allows your player’s data to be saved upon entering any scene and main menu exit button.

    However, I do not advise these method of loading the game. If the player were able to exit and re-enter the game by going back to the bench, this allows what is essentially a fast travel or get out of trouble mechanic for the player by just simply quitting the game. Or in another case it could accidentally reset the player’s progress.

    If the code does not work, feel free to tell me as this is simply theory and I have not yet tested the code.

    #13696
    Joseph Tang
    Moderator

    If you want to reset the saved data for the game,

    In SaveData.cs, create this method;

    public void ResetGame()
        {
            // Delete all save files if they exist
            if (File.Exists(Application.persistentDataPath + "/save.bench.data"))
                File.Delete(Application.persistentDataPath + "/save.bench.data");
    
            if (File.Exists(Application.persistentDataPath + "/save.player.data"))
                File.Delete(Application.persistentDataPath + "/save.player.data");
    
            if (File.Exists(Application.persistentDataPath + "/save.shade.data"))
                File.Delete(Application.persistentDataPath + "/save.shade.data");
    
            using (BinaryWriter writer = new BinaryWriter(File.OpenWrite(Application.persistentDataPath + "/save.player.data")))
            {
                playerHealth = 5;
                writer.Write(playerHealth);
                playerHeartShards = 0;
                writer.Write(playerHeartShards);
    
                playerMana = 1;
                writer.Write(playerMana);
                playerHalfMana = false;
                writer.Write(playerHalfMana);
                playerManaOrbs = 0;
                writer.Write(playerManaOrbs);
                playerOrbShard = 0;
                writer.Write(playerOrbShard);
                playerOrb0fill = 0;
                writer.Write(playerOrb0fill);
                playerOrb1fill = 0;
                writer.Write(playerOrb1fill);
                playerOrb2fill = 0;
                writer.Write(playerOrb2fill);
    
                playerUnlockedWallJump = false;
                writer.Write(playerUnlockedWallJump);
                playerUnlockedDash = false;
                writer.Write(playerUnlockedDash);
                playerUnlockedVarJump = false;
                writer.Write(playerUnlockedVarJump);
    
                playerUnlockedSideCast = false;
                writer.Write(playerUnlockedSideCast);
                playerUnlockedUpCast = false;
                writer.Write(playerUnlockedUpCast);
                playerUnlockedDownCast = false;
                writer.Write(playerUnlockedDownCast);
    
                playerPosition = new Vector2 (0,5);
                writer.Write(playerPosition.x);
                writer.Write(playerPosition.y);
    
                lastScene = "Cave_1";
                writer.Write(lastScene);
            }
    
            // Optionally reload the scene to reset the game
            SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
        }

    This will help delete all save files for the Bench, Shade and Player.
    However, your PlayerController will have to load a save file in it’s Start(). To avoid an error from occurring, we will copy and paste the code from SavePlayerData() into our newly created ResetGame().
    We will have to manually set the values to the ones that we want our Player to begin the game with, as seen above.
    In my case: I want my player to spawn at position (0, 5), thus i set playerPosition = new Vector2 (0, 5);, Or when I want my player to start with 5 health, playerHealth = 5;, And so on.
    Finally, we can call to reload the current scene to reset the scene if needed.

    As for a main menu button, we will need to use another script to call this SaveData.cs method through the Button’s On Click system.
    We will use the MenuFadeController.cs for this purpose:

    public void CallResetGame()
        {
            SaveData.Instance.ResetGame();
        }

    This method will help us to set up the On Click for our reset game button in the MainMenu scene, by calling CallResetGame() in the button’s inspector.

    #13697
    William Tyrant
    Participant

    Thanks alot i will try this

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: