Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Android kill the save file

Discussion in 'Scripting' started by AlexFreeman, Apr 28, 2017.

  1. AlexFreeman

    AlexFreeman

    Joined:
    Sep 29, 2016
    Posts:
    12
    The game save the data in OnApplicationPause event. It takes about 2 seconds to sarialize and to save the data. If I kill the game process in a task manager while the data is saving, the save file will be incorrect. And the game progress will be lost.
    I save data like this:

    Code (CSharp):
    1.        
    2.         public virtual void Save()
    3.         {      
    4.             BinaryFormatter bf = new BinaryFormatter();  
    5.             FileStream file = File.Open(FileName(), FileMode.OpenOrCreate, FileAccess.Write);
    6.  
    7.             bf.Serialize(file, this);
    8.             file.Close();
    9.         }
    10.  
    Can I lock the game in a memory while the data is saving? Or can I save data faster?
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,186
    You can't prevent someone from killing off the game on their own. If someone chooses to end it, that is their choice. This is the purpose of being able to kill things off (You see this in games all the time, don't close the game while the saving indicator is up)

    If your data is taking a long time to save, you either need to offer them another way to save or look at how much data you are saving and see if there is a way to cut down on the data. Or, you could save at different points in the game and use a saving indicator.