Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Saving data when system power is lost

Discussion in 'Editor & General Support' started by CrowbarSka, Feb 8, 2023.

  1. CrowbarSka

    CrowbarSka

    Joined:
    Dec 15, 2009
    Posts:
    192
    Hey folks, I'm struggling to solve a tricky problem. I'm working on a project for a public space where the game will be running all day on a Linux machine.

    At the end of each day, the venue staff will kill the power for all the PCs at the power source. They don't have time to manually shut them all down or exit the games one by one. This is a non-negotiable point.

    I need my game to save some data from time to time, but it seems like it gets corrupted when the power is killed. The Player.log file has some corrupted 'nonsense' strings at the end of it (apologies, I'm not too clued up on data management, writing to disk, etc).

    Until now my game had been using PlayerPrefs to save data. This works fine for my use case, and I call saves periodically throughout gameplay (every time a level is loaded, for example) and also OnApplicationQuit. But any changes made before the power is killed are not preserved. I did try removing the save on quit, but the same problem occurs.

    I've now also tried using the EasySave3 plugin (which I use in some other projects) but this has the same issue. When the power is killed, saving fails.

    Any ideas how I can fix this? I need data to be saved reliably without the game being exited or shut down cleanly.
     
  2. chemicalcrux

    chemicalcrux

    Joined:
    Mar 16, 2017
    Posts:
    717
    So you're only losing the data that was modified between the last call to
    PlayerPrefs.Save
    and the time that power is cut?

    Or are you losing everything because you wind up with corrupted data after the power cut?
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,713
    Journal the saves. Keep at least two going (A and B) and save periodically, then rotate them (round robin).

    If you don't want to journal, you can also back up any save by waiting a second and re-saving to a second backup file.

    In any case, date-stamp the files and run a simple hash / checksum on them and save that hash each time you save the file.

    This lets you:

    a) know which savefile is the latest

    b) know that it is intact

    PS - Sounds like an interesting project! Are you free to post any details here? (Just curious)
     
    halley and PraetorBlue like this.
  4. CrowbarSka

    CrowbarSka

    Joined:
    Dec 15, 2009
    Posts:
    192
    Oops, the cause of the issue was actually a stupid mistake in my save logic which was bypassing saving of these venue-specific settings until OnApplicationQuit (which obviously doesn't get called if you switch off the system power). So PlayerPrefs.Save() WAS being called very often, but I wasn't actually setting the values of certain bits of savable data until application shutdown. So they just weren't updated correctly.

    I've now made it save those settings along with all the other save data and it works fine now. ‍‍♂️ Apologies for the time-wasting!

    Thanks for the info though anyway, this is useful stuff to know.

    Thanks! It's actually a pretty straight-forward arcade cabinet version of my game Switch 'N' Shoot: Steam version, Arcade version. The arcade version has been out in the wild for a few years now but we're doing some updates. It's been a fun project to work on with lots of new challenges and considerations!
     
  5. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    1,863
    This is a standard approach used in embedded software by everything from action cameras to commercial jet airliners. Losing power during a firmware patch needs to not brick the device, so there's two copies of the firmware in separate chips/sectors, and the patch process will write to the opposite channel and only once completed mark it as the preferred channel for the next boot.
     
    CrowbarSka and Kurt-Dekker like this.