I believe that clarifying this topic is of primary importance to ensure a high standard of game development.
How do I…
I’m worried that players’ save files might get corrupted, especially if in-game purchases have been made. I’ve searched extensively on the site without finding the optimal solution: I’d like to understand the best way to create backups and verify their integrity.
What is the expected result
I would like to be able to verify the integrity of each save.
What is the actual result
Currently, I alternate saves between two slots (A and B). I also save a variable, SLOTPOINT, which controls which save to execute.
But I asked ChatGPT 4-Preview for the best solution, and it explained that this is it:
Use Atomic Save Operations
Save to a Temporary File First:
When saving the game, write your data to a temporary file (e.g., SAVE_TEMP).
This ensures that you aren’t overwriting your existing save file during the write operation, which could be interrupted (e.g., app crash, device shutdown).
Replace the Old Save File:
Once the temporary save is successful, replace the old save file (SAVE) with the new one.
This can be done by deleting the old file and renaming SAVE_TEMP to SAVE.
This operation is fast and minimizes the risk of file corruption.
Error Handling:
If the save operation fails at any point, the original SAVE file remains intact.
Always check for exceptions or errors during file operations and handle them appropriately.