- Home /
how to autosave your game?
im making a mobile game for andriod and i want to make a autosave system. i made a save system before but that was when you press a but that it saves but not an autosaver. like when you press your home button that it saves your game and when you start it again you still have your progress you had before you closed your game.
Answer by Larry-Dietz · Dec 01, 2019 at 09:39 PM
If you already know how to save and load, then making the save occur when they exit the game is pretty easy.
There is a method that will be called when the the user tries to quit the game.
void OnApplicationQuit()
{
//Call your save routine here
}
This will run when the application is about to quit. Assuming that they quit the application, and don't just terminate it. To account for this, you could also call the save routine every couple of minutes, assuming your save routine runs quickly enough that the player wont notice it.
In the start method of a script in the game scene, you could check if a saved game exists and load it from there.
Hope this helps, -Larry
okay it autosaves rn but now the only problem is the same with loading the game ive made a script to load the game but that was when you press the button load game its load the game but can you also make that when you start your game it autoloads the game? because i tryed "Void OnApplicationStart()" but then figured out that doesnt exist lol so can you also help me with that?
-patrick
Call your load routine from the Start or Awake method on a script in the first scene that needs your game data.
Great answer! I would just like to point out that, for people who find this thread in the future, if the save function is too slow you can always run it asynchronously on a different thread although things that could change during the save should be saved synchronously at the end! There are a lot of great documentation on multithreading for anyone interested!