Search Unity

OnApplicationPause and save data to file

Discussion in 'Scripting' started by CrazyApplesStudio, May 18, 2018.

  1. CrazyApplesStudio

    CrazyApplesStudio

    Joined:
    Jan 18, 2018
    Posts:
    25
    Hello , I recently found a somewhat annoying issue i have no idea how to approach. I am working on an android game where the GameManager saves the state when OnApplicationPause is triggered(is true), but it seems that some times the data is not save( i assume the application is paused before the code to save executes and thus nothing happenes) and on resume the values from the save files are the ones that were previously saved.

    Is there a way to make sure my SaveData call from OnApplicationPause is executed before the application is Paused by Android , if not what would be an alternative for this. I basically need the time and other variables to compute offline time as i do not want to run the app in the background(to avoid battery drain).

    Also , if i was to instead use variables (in case the assignment operations are actually executed 100%), would the OnApplicationQuit be usable to save the data to file? It is unclear if it behaves the same way as OnApplicationPause or does it actually execute all the code inside before it closes the App.

    Also what even do you use to trigger a save when the user closes the app from the task list , OnApplicationPause executes but OnApplicationQuit does not for me under this scenario.
     
  2. tranos

    tranos

    Joined:
    Feb 19, 2014
    Posts:
    180
  3. CrazyApplesStudio

    CrazyApplesStudio

    Joined:
    Jan 18, 2018
    Posts:
    25
    Hi , I am sure that topic has nothing to do with the question and even if it did , the information does not cover what I would like to know and asked in the topic.
    Anyway , i was inquiring about this event:
    https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnApplicationQuit.html
    How and when is executed etc.

    Anyway i found the issue that was causing me the bugs on my own, it was basically the fact that OnApplicationPause event is triggered on the scripts in random order, i thought (wrongly) it would respect script execution order , and thus i had weird data loads since there was variable assignments before loading(which is managed by a different script) etc.

    I would still appreciate some explanation on the 3 events as information about it seems scarce , so basically I am curios now if OnApplicationPause is enough , and what would be the use of OnApplicationFocus.
     
  4. tranos

    tranos

    Joined:
    Feb 19, 2014
    Posts:
    180
    For this, you could call your save function and then Application.Quit()

    Also it could be useful to see your code..
     
    Last edited: May 19, 2018
  5. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,147
    Check this out. Older manual entries mention that OnEnable(), Awake(), and Update() are called in the order the scripts are loaded and that this can be changed with the Script Execution Order settings. Starting with the entries for Unity 2017.3 this was changed to not make mention of those three.

    That entry would seem to suggest that the Script Order Execution settings only applied to those three. It's worth noting by the way that there is nothing in the release notes for Unity 2017.2 through 2017.3 that mentions any changes to it.

    https://docs.unity3d.com/Manual/class-MonoManager.html
    https://docs.unity3d.com/2017.2/Documentation/Manual/class-MonoManager.html

    One suggestion that appears to be the most likely to work is to have a background service running that handles the actual loading and saving of the game. Android will pause the game but the service will continue execution.

    https://stackoverflow.com/questions/38368762/start-android-service-from-unity3d-code
    http://jeanmeyblum.weebly.com/scripts--tutorials/communication-between-an-android-app-and-unity
     
    Last edited: May 19, 2018
    Doug_B likes this.
  6. CrazyApplesStudio

    CrazyApplesStudio

    Joined:
    Jan 18, 2018
    Posts:
    25
    Tranos no offense but we don't really seem to be on the same page , I am inquiring about events and when/how they are triggered , not about Application.Quit which you seem to keep pointing.
     
  7. CrazyApplesStudio

    CrazyApplesStudio

    Joined:
    Jan 18, 2018
    Posts:
    25
    Thank you , will look in to it , i can confirm that the events are triggered in random order and ignore Script Execution Order.
    Currently i solved it by moving all the OnApplicationPause calls in the GameManager so calling all the methods that i need in order in one function avoids this .

    Now it remains to find what is the difference between OnApplicationPause and OnApplicationFocus on Android.
    I would also like to point that the documentation on all these events is extremely scarce.