- Home /
Questing Regarding Save and Load System
Hi, I have a bit trouble of doing my code for Save and Load System... So i have this code for my save and load system
public class SaveLoad {
public static List<Game> savedGames = new List<Game> ();
public static void Save(){
savedGames.Add (Game.current);
BinaryFormatter bf = new BinaryFormatter ();
FileStream file = File.Create (Application.persistentDataPath + "/savedGames.dat");
bf.Serialize (file, SaveLoad.savedGames);
file.Close ();
}
public static void Load(){
if (File.Exists(Application.persistentDataPath + "/savedGames.dat")){
BinaryFormatter bf = new BinaryFormatter ();
FileStream file = File.Open (Application.persistentDataPath + "/savedGames.dat", FileMode.Open);
SaveLoad.savedGames = (List<Game>)bf.Deserialize (file);
file.Close ();
}
}
}
[System.Serializable]
public class Game {
public static Game current;
}
I was hoping that I able to save my playerpositions, level and stats but i don't know how and where to put the code...
Please format your code as code.
Your Game class needs variables that can store the data you want, like player positions etc.
Sorry, I updated the code
You mean like this?
public int Lv;
public int Xp;
public Vector2 position;
public int Atk;
public int Hp;
public int Def;
public Character()
{
this.Lv = PlayerStats.Pstat.currentLvl;
this.Xp = PlayerStats.Pstat.currentExp;
this.position = Player$$anonymous$$ovement.P$$anonymous$$ov.lastmoving;
this.Atk = PlayerStats.Pstat.curAtk;
this.Hp = PlayerStats.Pstat.curHp;
this.Def = PlayerStats.Pstat.curDef;
}
Yes... Although you won't be able to serialize Unity-specific classes like Vector2. You will have to either split the x,y values into seperta float variables, or write a SerializationSurrogate that the BF can use to automatically convert the Vector2.
Your answer
![](https://koobas.hobune.stream/wayback/20220612143858im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
PlayerPrefs issue 2 Answers
Save progress in WebGL game (C#) 1 Answer
PlayerPrefs Script Problem 1 Answer