Search Unity

how can i make certain values continue when the game/app is closed?

Discussion in 'Getting Started' started by Sylvir, Apr 22, 2015.

  1. Sylvir

    Sylvir

    Joined:
    Mar 21, 2015
    Posts:
    396
    certain big incremental games such as adventure capitalist do this, and i am wondering if there is a way to implement that in code through unity as well? i know there is a "DontDestroyOnLoad" command, but I am un sure if there are any kinds of "DontDestroyOnClose Or Load" type of commands. Thanks!
     
  2. blizzy

    blizzy

    Joined:
    Apr 27, 2014
    Posts:
    775
    What exactly are you talking about? Keeping things running after the program has quit? That's not possible. The usual way to "calculate" things in the background (say, "earning money even while your computer is off") is to just calculate them when the program starts. For that you just need to save the date and time when the program exits, and calculate the difference at start.
     
  3. Sylvir

    Sylvir

    Joined:
    Mar 21, 2015
    Posts:
    396
    oh ok thanks, so basically i just do that, and then whatever amount they were earning when they logged off i calculatei t at the start. could you give me an example of how i might save date and time then call it at the start up ?
     
  4. wetcircuit

    wetcircuit

    Joined:
    Jul 17, 2012
    Posts:
    1,409
    Sylvir likes this.
  5. blizzy

    blizzy

    Joined:
    Apr 27, 2014
    Posts:
    775
    The easiest way I can think of is to just take the current time and date in, say, seconds since the epoch, store that into PlayerPrefs, and then at the start of your program, do the same, and calculate the time difference from that:

    Code (csharp):
    1. // on quit
    2. long date = DateTime.UtcNow.Ticks / 10000 / 1000;
    3. PlayerPrefs.SetInt("lastDate", date);
    4.  
    5. // on start
    6. long nowDate = DateTime.UtcNow.Ticks / 10000 / 1000;
    7. long lastDate = PlayerPrefs.GetInt("lastDate");
    8. long secondsSinceLastDate = nowDate - lastDate;
    I'm not sure if PlayerPrefs.SetInt() is big enough to actually hold the value. You might want to use bigger units than seconds, or use something like this to use a later epoch date: http://stackoverflow.com/a/9453127
     
    Sylvir likes this.
  6. Sylvir

    Sylvir

    Joined:
    Mar 21, 2015
    Posts:
    396
    awesome, thank you, i will work with this and see what i can figure out. Aslo thank you wetcircuit for the link on player prefs. for some reason i am having a hard time understanding how to "call " the saved player prefs on opening the game. I will see if i can get it re watching the unity video again this time. thanks! hopefully this will help me work it out now.
     
    wetcircuit likes this.
  7. blizzy

    blizzy

    Joined:
    Apr 27, 2014
    Posts:
    775
    You don't need to load the PlayerPrefs data. The class does it on its own.
     
  8. Sylvir

    Sylvir

    Joined:
    Mar 21, 2015
    Posts:
    396
    oh ok, so if i put in something like this..

    Code (CSharp):
    1.     void OnDestroy()
    2.     {
    3.         PlayerPrefs.SetFloat ("FISH", FishClick.fish);
    4.         PlayerPrefs.SetFloat ("FISHPERTAP", FishClick.fishPerTap);
    5.             PlayerPrefs.Save ();
    6.     }
    then that should save when i close the game automatically for the 2 values fish and fishpertap ?
     
  9. blizzy

    blizzy

    Joined:
    Apr 27, 2014
    Posts:
    775
    I think it should work.
     
  10. Sylvir

    Sylvir

    Joined:
    Mar 21, 2015
    Posts:
    396
    cool, thanks! ill play around with that and see :) going to build the project now and then run it and see if it saves thanks!
     
  11. Sylvir

    Sylvir

    Joined:
    Mar 21, 2015
    Posts:
    396
    doesnt seem to be saving, any idea what i would need to change ? for some reason this using player prefs to save thing has been tricky for me to work out. doesnt seem like it is too tricky from all the videos ive watched though
     
  12. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,145
    There is a free utility on the Asset Store that allows you to create, modify, and delete PlayerPrefs for your project directly in the editor. I tried running the code snippet you put above (replacing the variable names with actual values) and they showed up in the asset's editor window (Window -> PlayerPrefs Overview).

    https://www.assetstore.unity3d.com/en/#!/content/31230
     
  13. Sylvir

    Sylvir

    Joined:
    Mar 21, 2015
    Posts:
    396
    thanks ill give that a look see and try and work out what im missing
     
  14. Sylvir

    Sylvir

    Joined:
    Mar 21, 2015
    Posts:
    396
    how do i call it correctly to load when i start again? think thats where im doing something wrong
     
  15. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,145
    Load the values from the prefs?

    Code (csharp):
    1. FishClick.fish = PlayerPrefs.GetFloat("FISH");
    2. FishClick.fishPerTap = PlayerPrefs.GetFloat("FISHPERTAP");
     
  16. Sylvir

    Sylvir

    Joined:
    Mar 21, 2015
    Posts:
    396
    thank you