Search Unity

  1. Are you interested in providing feedback directly to Unity teams? Sign up to become a member of Unity Pulse, our new product feedback and research community.
    Dismiss Notice

Help Wanted How can I control the save games if to save using only with key press down or also automatic points?

Discussion in 'Scripting' started by shamenraze1988, Jul 8, 2021.

  1. shamenraze1988

    shamenraze1988

    Joined:
    Nov 24, 2020
    Posts:
    208
    This is the method I'm using for saving :

    Code (csharp):
    1.  
    2. public IEnumerator SaveWithTime()
    3.     {
    4.         yield return new WaitForSeconds(timeToStartSaving);
    5.  
    6.         Save();
    7.  
    8.         StartCoroutine(fadeInOutSaveGame.OverAllTime(savingFadeInOutTime));
    9.     }
    10.  
    And this is an example of how I'm using it in another script in places in the game that I want it will save :

    I first make a reference to the script where the method SaveWithTime is :

    Code (csharp):
    1.  
    2. public SaveLoad saveLoad;
    3.  
    And then saving inside the Update or event when some conditions happen:

    Code (csharp):
    1.  
    2. private void Update()
    3.     {
    4.         if(dimLights.lightsDimmed == true && lightsDim == false && unlockCarte.HasOpened() == true
    5.              && MenuController.LoadSceneForSavedGame == false || dimLights.lightsDimmed == true && lightsDim == false && unlockCarte.HasOpened() == true /*&& playingInGameSceneController.newGameHasLoaded == true*/)
    6.         {
    7.             StartCoroutine(saveLoad.SaveWithTime());
    8.         }
    9.     }
    10.  
    But now for testing, I want to use a key press down in this case the left control button for saving so I can decide when to save the game so I created this small script :

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class SaveGameTest : MonoBehaviour
    7. {
    8.     public SaveLoad saveLoad;
    9.  
    10.     [Tooltip("If true will save manually only by the left control key.")]
    11.     public bool cancelAutoSaving = false;
    12.  
    13.     private void Start()
    14.     {
    15.  
    16.     }
    17.  
    18.     private void Update()
    19.     {
    20.         if (Input.GetKeyDown(KeyCode.LeftControl))
    21.         {
    22.             StartCoroutine(saveLoad.SaveWithTime());
    23.         }
    24.     }
    25. }
    26.  
    The problem is because I have ins some places in the game auto-saving by calling the method SaveWithTime when I save with the left control key the auto-saving is overriding the saved game file.

    I want to do something generic and easy. Maybe something inside the method SaveWithTime some flag some public static flag may be that if it's a true loop, find all the saving places and save only this with key pressing like the left control key and disable all the auto-saving places. and if the flag is false then save either with the key press down or with the auto-saving.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    18,293
    First decide on the behavior you want.

    Some games have one single save slot and both demand- and auto-save goes in there, the latest one overwriting the previous. This is common in console games, especially with small memory cart platforms.

    Other games have dedicated slots for demand-saving and dedicated slots for auto-saving.

    Obviously in that case they need more UI to offer the user which one to reload when continuing.

    Once you decide on that you can decide how to respond to the user's save intention and the timer's save intention.
     
    shamenraze1988 likes this.
  3. shamenraze1988

    shamenraze1988

    Joined:
    Nov 24, 2020
    Posts:
    208

    I did already a UI for saving games and loading I did it in another project and it's working fine.
    I just thought to make something for my self something for more kind of debugging so I can save anywhere any time I want. Not the player in the game it's for me. In this game the saving behavior is automatic saves in specific points in the game I decide, I just wanted to make myself something easier to be able to save at any time any place but I don't want to override or remove the autosaves places I just want to kind of disable this places temporarily.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    18,293
    I would just put the timer autosave on a GameObject and let it do its thing until you save manually, then Destroy() it so it won't save again until you restart. Or make it permanent by writing something to PlayerPrefs (for instance) saying "autosaves suspended."
     
    shamenraze1988 likes this.
unityunity