Checkpoint automatic save/load

I am making a 2D sidescroller game. When the player walks over a checkpoint I want this position to be stored as the spawning position, after the next death.

I don’t want to set all variables back to their original values, whenever a death occurs.

What I really want is to save this exact game-state when you pass the checkpoint. And then if player dies → load state → change playerPosition, set lives–.

What is the best way to do this? :slight_smile:

PlayerPrefs is the best option for you. Heres an example for their lives:

public int livesLeft = 3;

void OnGUI () {
    if (livesLeft == 0) {
       if (GUI.Button (new Rect (Screen.width / 2 - 50, Screen.height / 2 + 50, 100, 25), "SAVE")) {
			
       PlayerPrefs.SetString ("Player Lives", livesLeft);
       }

       if (GUI.Button (new Rect (Screen.width / 2 - 50, Screen.height / 2 + 50, 100, 25), "SAVE")) {
           livesLeft == PlayerPrefs.GetString ("Player Lives");	
       }
    }
}

It would be similar for their position too.

You could actually call a function (say its called RespawnPlayer) if the lives equal zero then do the load save.

Two general strategies exist

  • Store the data on player lives and position
  • Reload the level (This will reset everything)
  • Move the player to the appropriate position

or

  • Store the state of every variable at the check point
  • On the players death move reset everything back to the checkpoint state

Check out the tutorial on saving and persistent data for methods to save.