Daniel Lip
Daniel Lip

Reputation: 11335

How should I reset all the object in fact the whole world to it's original state when restarting a new game?

The problem is when I'm saving the game once then when starting a new game without quitting the game first it's starting the new game but the camera for example and player positions are not the same as when starting the new game at first time. Maybe the camera is the same but the player position is not.

It looks like when making a new game WITHOUT QUITTING the game first after saved the game once not reset everything.

I have two scenes in the hierarchy Main Menu and Game. Under the Game I have all the game objects.

In the Main Menu I have this script with two methods that I call from a buttons onclick events one for new game one for loading :

public void ClickNewGameDialog(string ButtonType)
        {
            if (ButtonType == "Yes")
            {
                loading = false;
                newGameDialog.SetActive(false);
                StartCoroutine(sceneFader.FadeAndLoadScene(SceneFader.FadeDirection.In, _newGameButtonLevel));
            }

            if (ButtonType == "No")
            {
                GoBackToMainMenu();
            }
        }

        public void ClickLoadGameDialog(string ButtonType)
        {
            if (ButtonType == "Yes")
            {
                loading = true;
                newGameDialog.SetActive(false);
                StartCoroutine(sceneFader.FadeAndLoadScene(SceneFader.FadeDirection.In, _newGameButtonLevel));               
                
            }

            if (ButtonType == "No")
            {
                GoBackToMainMenu();
            }
        }

And the FadeAndLoadScene method :

public IEnumerator FadeAndLoadScene(FadeDirection fadeDirection, string sceneToLoad)
    {
        yield return Fade(fadeDirection);
        SceneManager.LoadScene(sceneToLoad);
    }

On a new game the string is the same when loading "Game" since I have one scene for the Game.

This is a class I did just to be able easy to call my Save and Load methods :

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SaveLoad : MonoBehaviour
{
    private static SaveGame saveGame;

    private void Awake()
    {
        // Create your temporary save game
        saveGame = new SaveGame();
        SaveMaster.SetSlot(0, true, saveGame);
    }

    public static void Save()
    {
        // Is it using a temporary slot?
        if (SaveMaster.GetActiveSlot() == 0)
        {
            if (SaveFileUtility.IsSlotUsed(1))
                SaveMaster.DeleteSave(1); // Removes the save game that was available.
            SaveMaster.SetSlot(1, false, saveGame); // Sets the slot to the first one, while keeping your save game.
        }

        SaveMaster.SyncSave();
        SaveMaster.WriteActiveSaveToDisk();
    }

    public static void Load()
    {
        SaveMaster.SetSlot(1, true);
    }
}

And this is a short scene clip when starting a new game and here I'm doing the save and loading :

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Cinemachine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using SpeedTutorMainMenuSystem;

public class PlayingInGameScenesController : MonoBehaviour
{
    public CinemachineFreeLook[] freeLookCams;
    public CinemachineVirtualCamera[] virtualCams;
    public LockController lockController;
    public GameObject uiSceneText;
    public GameObject player;
    public float transitionSpeed = 5f;
    public float thresHold = 0.1f;
    public bool talkingProcess = true;
    public FadeInOutSaveGameText fadeInOutSaveGame;

    private bool newGame = true;

    private void Start()
    {
        
    }

    public void PlayingSceneInGame()
    {
        PlayingSceneStatesControls(true);
        StartCoroutine(ScenePlayingTime());
    }

    private void Update()
    {
        if (SceneManager.GetActiveScene().name != "Main Menu" && newGame == true && MenuController.loading == false)
        {
            PlayingSceneInGame();
            newGame = false;
        }

        if (SceneManager.GetActiveScene().name != "Main Menu" && MenuController.loading == true)
        {
            SaveLoad.Load();
        }
    }

    private void LateUpdate()
    {
       
    }

    private void PlayingSceneStatesControls(bool LockState)
    {
        lockController.LockControl(LockState);

        if (LockState == true)
        {
            uiSceneText.SetActive(true);
        }
        else
        {
            talkingProcess = false;
            uiSceneText.SetActive(false);
        }
    }

    IEnumerator ScenePlayingTime()
    {
        yield return new WaitForSeconds(10);

        PlayingSceneStatesControls(false);

        freeLookCams[0].enabled = false;
        freeLookCams[1].enabled = true;

        var brain = Camera.main.GetComponent<CinemachineBrain>().m_DefaultBlend.m_Time;

        StartCoroutine(SaveAfterSomeTime());
        
    }

    IEnumerator SaveAfterSomeTime()
    {
        yield return new WaitForSeconds(15f);

        SaveLoad.Save();

        StartCoroutine(fadeInOutSaveGame.OverAllTime(5f));
    }
}

The game start and after overall 25 seconds it's making automatic save game. Then when I'm loading the game it's loading the scene then loading the saved game and position the player where it was saved.

The problem is when I'm making Load Game and then escape to main menu and then making New Game then the New Game start with the player from the saved game position instead from the original position. And when starting a new game I'm not doing any Loading :

This is for a new game :

if (SceneManager.GetActiveScene().name != "Main Menu" && newGame == true && MenuController.loading == false)
        {
            PlayingSceneInGame();
            newGame = false;
        }

This is for the Loading :

if (SceneManager.GetActiveScene().name != "Main Menu" && MenuController.loading == true)
        {
            SaveLoad.Load();
        }

If I will make a save and load the game and then Quit the game run the over again a will make a new game first it will start the game as new fine. The problem is while in the game when making save then load then new game then the new game never reset the player position to the original.

Should I make some kind of global world state saving and load the state each time when making a New Game?

For now I'm saving only the Player state :

Player State Saving Components

I thought when you start a new game loading the Game scene without loading saved games it should start the game from zero everything should be rest to the original. but it's not reset the Player position and rotation.

It does if I quit the game first but not while the game is still running.

I tried this but it's not working yet :

public CinemachineFreeLook[] freeLookCams;
    public CinemachineVirtualCamera[] virtualCams;
    public LockController lockController;
    public GameObject uiSceneText;
    public GameObject playerStart;
    public float transitionSpeed = 5f;
    public float thresHold = 0.1f;
    public bool talkingProcess = true;
    public FadeInOutSaveGameText fadeInOutSaveGame;

    private bool newGame = true;
    private Vector3 playerOriginalPosition;
    private Vector3 playerOriginalScale;
    private Quaternion playerOriginalRotation;

    private void Start()
    {
        playerOriginalPosition = playerStart.transform.position;
        playerOriginalScale = playerStart.transform.localScale;
        playerOriginalRotation = playerStart.transform.rotation;
    }

    public void PlayingSceneInGame()
    {
        PlayingSceneStatesControls(true);
        StartCoroutine(ScenePlayingTime());
    }

    private void Update()
    {
        if (SceneManager.GetActiveScene().name != "Main Menu" && newGame == true && MenuController.loading == false)
        {
            playerStart.transform.position = playerOriginalPosition;
            playerStart.transform.localScale = playerOriginalScale;
            playerStart.transform.rotation = playerOriginalRotation;

            PlayingSceneInGame();
            newGame = false;
        }

Upvotes: 1

Views: 311

Answers (1)

Pino De Francesco
Pino De Francesco

Reputation: 756

I'm not sure what that Savable component on the player is, but I assume from its name that is a component that saves and loads the state of the player automatically, which obviously overrides the player position when the scene starts.

Simply define a Player_Start transform in the scene (an empty game object) and add a simple logic that, if the game is a New_Game then place the player at Player_Start.

Upvotes: 2

Related Questions