Auto Save/Load Games

👍

Reminder - Keep Daily Backups

When working on a strategy game with a kit as big as the Complete Kit - always keep a working daily backup! Save yourself the trouble of rolling-back changes and losing work.

What is auto save and auto load?

The load/save sequences are not automated by default to give you freedom to test and develop further. When you're ready to publish your game, we have commented functions in SaveLoadMap and SaveLoadWWW that allow you to automatically save or load content on game start or exit.

What is the technical process for saving my game?

StepDescription
1. Local SaveSaveLoadMap.cs SaveGame() function stores all the player variables into a file. For Unity developers on Windows, this is: C:/Users/user/AppData/LocalLow/AStarterKits/StrategyKit/MyMapID.txt
For Mac, it is ~/Library (look for the unity AStrategyKit plist)
2. Server UploadSaveLoadWWW.cs posts a copy of the local player save game file to the server scripting.
3. Server VerficiationSaveLoadWWW.cs requests the player file from the server and verifies that both the starting and ending lines are present to confirm the upload was successful.

🚧

How to Save the Game During Development Testing

Because of this three-step process, when testing without auto-save and auto-load, to save online you must first press Local Save in the game settings and then you can press Server Save. A local game save file is necessary first before server saving.

Why is auto save/load not enabled by default?

For development, to test server saving in the game kit, after you have entered your license code onto the GameManager > SaveLoadWWW Inspector, you must press Local Save first in the game to save the player data locally. Then press Server save to save a copy online.

Automatic save/load is not enabled by default to give developers the freedom to test and develop their game further. Each session you play in the Unity editor performs like a new game session.

To test player sessions, you can load and save existing player maps by customizing the Player ID in the GameManager > SaveLoadWWW Inspector inside Unity.

How to enable automated local loading on game start?

Open Scripts/Save/SaveLoadMap.cs and locate the following lines in the Start() function:

//LoadFromLocal ();	
//automatic local load at startup - for local testing; for multiplayer, 
//the user must both upload/download from server to receive the attack results

Uncomment the LoadFromLocal() line like below, and save the file. The game will automatically check for a local player saved data file on application start.

LoadFromLocal ();	
//automatic local load at startup - for local testing; for multiplayer, 
//the user must both upload/download from server to receive the attack results

How to enable automated local game saving exit?

Open Scripts/Save/SaveLoadMap.cs and locate the following OnApplicationQuit() function:

void OnApplicationQuit() //autosave
{
	//if(!((Relay)relay).pauseInput)//check if the user is doing something, like moving buildings
	//SaveGameLocalFile();
}

The above function is used by Unity to determine when an application is quitting. It checks to make sure the user is not doing an action that would corrupt the save data, like moving buildings at the same moment. Uncomment the two lines in this function like the example below to enable automated saves on application quit. To test, you'll need to make a build, and then press the Exit button in settings or close the application.

void OnApplicationQuit() //autosave
{
	if(!((Relay)relay).pauseInput)//check if the user is doing something, like moving buildings
	SaveGameLocalFile();
}

What about mobile devices?

For Android mobile devices - pressing the home button pauses the application, it doesn't quit the application. Opening the application again will not trigger a load because it's technically resuming from a paused state, not opening for the first time.

Luckily, Unity has two additional functions that will help you in these situations. If you're building for mobile games, you want to add them too to SaveLoadMap.cs at the bottom next to void OnApplicationQuit()

// For Android Home Button Save / Load
// See Unity Docs: https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnApplicationPause.html
 void OnApplicationFocus()
    {
       LoadFromLocal ();	
    }

    void OnApplicationPause()
    {
	     SaveGameLocalFile();
    }

How to enable automated server loading on game start?

❗️

On application start, load either locally or from the server, not both!

If you have already enabled automated local save/load above - you cannot also enable automated server save/load. A load error will trigger in the Development console warning you that the second attempt was blocked to prevent loading a different save file on top of an existing base. Collision errors will occur if objects are placed on top of other objects.

Open Scripts/Save/SaveLoadWWW.cs and locate the following lines in the Start() function to enable your game to automatically check the server for the player's game copy (including any battle results if enemies have attacked their base while the player was gone)

//StartCoroutine ("ServerAutoLoad");	//automatic server load at startup - prevents user from building and then loading on top

Uncomment the StartCoroutine ("ServerAutoLoad") line like the example below, and save the file. The game will automatically check online for a server save copy on application start.

StartCoroutine ("ServerAutoLoad");	//automatic server load at startup - prevents user from building and then loading on top

How to enable automated server saving on exit?

Open Scripts/Save/SaveLoadWWW.cs and add the following function at the very end, like the SaveLoadMap.cs example above for local loading.

void OnApplicationQuit() //autosave
{
	if(!((Relay)relay).pauseInput)//check if the user is doing something, like moving buildings
    
    
   // If the development console returns a message:
    // "Local save file not found. Save locally first"
	SaveToServer();
  
  
}

❗️

A local save is necessary first before server saving

A local game save file is necessary first before server saving.

Console message: "Local save file not found. Save locally first"

If you get a message in the development console that says "Local save file not found. Save locally first" you need to trigger a local save first before you can trigger a server save. Follow these steps:

  1. Add the following line at the top of Scripts/Save/SaveLoadWWW.cs
//Add the following line at the top of Scripts/Save/SaveLoadWWW.cs
private Component saveLocalMapFirst;
  1. Add the following line to the SaveLoadWWW.cs Start function. This will help us call the SaveGameLocalFile() function in the next step
// Add the following line to Start()
void Start(){
 saveLocalMapFirst = GameObject.Find ("SaveLoadMap").GetComponent<SaveLoadMap> ();
}
void OnApplicationQuit() //autosave
{
	if(!((Relay)relay).pauseInput)//check if the user is doing something, like moving buildings 
    
   // Since the development console returns a message:
   // "Local save file not found. Save locally first"
   // We call the SaveLoadMap script and save locally first
    	((SaveLoadMap)saveLocalMapFirst).SaveGameLocalFile();
   // Wait a couple seconds, just in case
			yield return new WaitForSeconds (2);
 
   // Now attempt a server save
			SaveToServer(); 
}
/*
	// For Mobile games on android devices you also may want to
  // save on application pause, here's a modified version of
  // what you'll find in the next section for saving on application pause
    void OnApplicationPause()
    {
    	((SaveLoadMap)saveLocalMapFirst).SaveGameLocalFile();
			yield return new WaitForSeconds (2);
			SaveToServer();
    }
    
*/

What about server load/save mobile devices?

❗️

On application start, load either locally or from the server, not both!

If you have already enabled automated local save/load above - you cannot also enable automated server save/load. A load error will trigger in the Development console warning you that the second attempt was blocked to prevent loading a different save file on top of an existing base. Collision errors will occur if objects are placed on top of other objects.

Just like the mobile devices section above for local loading/saving, there's two additional cases to detect - application pausing and application resuming if you want to do mobile games. Add the following to the bottom of SaveLoadWWW.cs to automatically save and load in these situations

// For Android Home Button Save / Load
// See Unity Docs: https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnApplicationPause.html
 void OnApplicationFocus()
    {
       StartCoroutine ("ServerAutoLoad");
    }

    void OnApplicationPause()
    {
			SaveToServer();
    }