- Home /
How do I clear a json save file when the user clicks on new game?
I am making a game and I want it so that if the user clicks on New Game it will clear the save file and they can start out from the beginning. How would I do that? I'll add my save scripts for reference.
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class GameInfo : MonoBehaviour {
public int Lives = 3;
public int Score = 0;
void Start()
{
LoadPlayer();
}
public void AddPoints()
{
Score += 10;
}
public void LoseLive()
{
Lives -= 1;
}
public void SavePlayer()
{
SaveSystem.SavePlayer(this);
}
public void LoadPlayer()
{
GameStatus data = SaveSystem.LoadPlayer();
Lives = data.Lives;
Score = data.Score;
}
}
using System.Collections; using System.Collections.Generic; using UnityEngine;
[System.Serializable] public class GameStatus { public int Lives; public int Score;
public GameStatus(GameInfo player)
{
Lives = player.Lives;
Score = player.Score;
}
}
using UnityEngine; using System.IO; using System.Runtime.Serialization.Formatters.Binary;
public static class SaveSystem {
public static void SavePlayer(GameInfo player)
{
BinaryFormatter formatter = new BinaryFormatter();
string path = Application.persistentDataPath + "/game.data";
FileStream stream = new FileStream(path, FileMode.Create);
GameStatus data = new GameStatus(player);
formatter.Serialize(stream, data);
stream.Close();
}
public static GameStatus LoadPlayer()
{
string path = Application.persistentDataPath + "/game.data";
if (File.Exists(path))
{
BinaryFormatter formatter = new BinaryFormatter();
FileStream stream = new FileStream(path, FileMode.Open);
GameStatus data = formatter.Deserialize(stream) as GameStatus;
stream.Close();
return data;
}
else
{
Debug.LogError("Save file not found in " + path);
return null;
}
}
}
Answer by Hellium · Feb 20, 2021 at 12:34 AM
Make your
SavePlayer
method take aGameStatus
as argument instead of aGameInfo
Add a default constructor in your
GameStatus
class initializingLives
&Score
to 0Change
SaveSystem.SavePlayer(this);
toSaveSystem.SavePlayer(new GameStatus(this));
Call
SaveSystem.SavePlayer(new GameStatus());
when the New Game button is clicked
@Hellium hey I think I believe I did what you ask and it is returning some errors. The SaveSystem.SavePlayer(new GameStatus()); "new GameStatus" and the SaveSystem.SavePlayer(this); is not working in my GameInfo. What should I do?
Here this is the script I am talking about.
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class GameInfo : MonoBehaviour {
public int Lives = 3;
public int Score = 0;
void Start()
{
LoadPlayer();
}
public void AddPoints()
{
Score += 10;
}
public void LoseLive()
{
Lives -= 1;
}
public void SavePlayer()
{
SaveSystem.SavePlayer(this);
}
public void LoadPlayer()
{
GameStatus data = SaveSystem.LoadPlayer();
Lives = data.Lives;
Score = data.Score;
}
public void newGame()
{
SaveSystem.SavePlayer(new GameStatus());
}
}
It's almost impossible to help without the errors and the code throwing those errors.
Make sure you have added a default constructor to the GameStatus class = a constructor taking no argument.
You also need to change SaveSystem.SavePlayer(this);
to SaveSystem.SavePlayer(new GameStatus(this));
@Hellium okay I'll be honest with you I don't know what a default constructor is can you send me an example.
Your answer
Follow this Question
Related Questions
Save and Load 2 Answers
SaveStates in a platformer 0 Answers
persistantdata deleted on steam build 0 Answers
Save picture from camera with zoom 0 Answers
How would you send binary data over web? 2 Answers