Im making an old school arcade for android where you have to get the highest score between levels. At each level you can save the progress. If you die and have 0 lives left your save file gets deleted. However to prevent cheating I want the game to erase the save file when the player quits the game mid level instead of going back to the last save. What is the code for checking if the player force closed the game?
Only allow saving on quit and delete the save on load.
A somewhat common approach to this sort of problem is to quit the game when you save. So for your case, when they player try to save the game they could be presented with two options:
- Save and quit
Then when the player returns to the game to load their save you can pop up a warning to the effect of "loading this save will delete it, are you sure you want to continue?"
Note: It should be noted that games with this sort of save system will typically have communities that share how to backup and restore your save in order to bypass this limitation. So while casual players may abide by your save restrictions, people who are actually fans of your game will likely try to circumvent it. As other answers have mentioned, often times it's not worth it to go through fancy hoops to protect offline single player games.
you cant prevent cheating in a offline game in any system of storage.
if you store it in ram, a hacker can read it and change it. look for trainers that are built.
even if you encrypt the saved or any important data, its possible to read it when its decrypted or before encryption.
if you see that online games cant be cheated because all logic and data is on server and user client only can send inputs.
even if your goal is not cheating, deleting a file and making a new one is not a good practice. you should manipulate contents in the file.
conclusion: for a game that is not still big enough to be valuable for cheating, dont worry about cheating. just make it, you can make it safe when its needed.