- Home /
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? :)
Answer by thornekey · Aug 11, 2014 at 03:15 AM
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.
Hmm ok, PlayerPrefs you say. But how can I do the actual saving/loading.
I can't see the difference between setting a string in playerprefs and just creating a normal string.
This seems to be saving the position of all enemies and the player in variables - and then reposition the objects if the player dies.
Ok I see, Stores and accesses player preferences between game sessions. This is not what I need though. I want ALL the values in the scene to be reset, EXCEPT FOR TWO. playerPosition and Lives should NOT be reset, but everything else
When you close your game, and re open it click load and it will load the player prefs of how many lives you have left. test it out. This is what you want. Even i see that lol. It will only save the lives and position of the player if you set it to do that.
you dont have to have it in a button. $$anonymous$$aybe make it so that it sets the player prefs when you go through a collider (a checkpoint).. then IF you reach 0 lives, load the player prefs..
public int livesLeft = 3;
void Update () {
if (livesLeft == 0) {
PlayerPrefs.GetString ("Player Lives");
////load the location player prefs float (PlayerPrefs.GetFloat)
}
if (collision with the collider checkpoint) {
PlayerPrefs.GetString ("Player Lives", livesLeft);
//save the checkpoint location in a float
}
}
Answer by Kiwasi · Aug 11, 2014 at 04:07 AM
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.
Your answer

Follow this Question
Related Questions
2D Game Checkpoint System 1 Answer
Checkpoints in a platformer 1 Answer
Checkpoint in c# 0 Answers
save funtion c# 1 Answer
problems with check point in unity 0 Answers