- Home /
Saving and Loading w/ BinaryFormatter
Hello, I am working on a save system the revolves around Binary Formatting and I am struggling to load default variables if a player was new (like costs and etc). This is what I got so far for an example (please DO NOT recommend PlayerPrefs, I am moving away from that):
Main:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class IncrementSaveTest : MonoBehaviour
{
public double TestDouble = 1;
public void Start()
{
PlayerData data = SaveSystem2.LoadPlayer();
}
public void Update()
{
TestDouble *= 1.001;
SaveSystem2.SavePlayer(this);
}
}
Data: using System.Collections; using System.Collections.Generic; using UnityEngine;
[System.Serializable]
public class PlayerData
{
public double TestDouble;
public PlayerData (IncrementSaveTest player)
{
if (TestDouble == 0)
TestDouble = 10; // the number im trying to start off with
else
TestDouble = player.TestDouble;
}
}
SaveSystem: using UnityEngine; using System.IO; using System.Runtime.Serialization.Formatters.Binary;
public static class SaveSystem2
{
public static void SavePlayer(IncrementSaveTest player)
{
BinaryFormatter formatter = new BinaryFormatter();
string path = Application.persistentDataPath + "/player68.test";
FileStream stream = new FileStream(path, FileMode.Create);
PlayerData data = new PlayerData(player);
formatter.Serialize(stream, data);
stream.Close();
}
public static PlayerData LoadPlayer()
{
string path = Application.persistentDataPath + "/player68.test";
IncrementSaveTest player = new IncrementSaveTest();
if (File.Exists(path))
{
BinaryFormatter formatter = new BinaryFormatter();
FileStream stream = new FileStream(path, FileMode.Open);
PlayerData data = formatter.Deserialize(stream) as PlayerData;
stream.Close();
return data;
}
else
{
Debug.LogError("Save file not found in " + path);
PlayerData data = new PlayerData(player);
data.TestDouble = 10; // another attempt at trying to load with this number on first time load...
return null;
}
}
}
Anyone have any advice? Went off of Brackeys tutorial and wish he discussed default variables, that's why i'm here! :P Thanks, Zach.
Answer by Kennai · Jun 19, 2019 at 07:25 AM
Hello, @ZachAttack9280
Here is your flat class to store data
[System.Serializable]
public class PlayerData
{
public float health, mana;
public PlayerData (IncrementSaveTest player)
{
this.health = player.health;
this.mana = player.mana
}
public void Load(IncrementSaveTest player)
{
player.health = this.health;
player.mana = this.mana;
}
}
This is your save system
public static class SaveSystem2
{
public static void SavePlayer(IncrementSaveTest player)
{
BinaryFormatter formatter = new BinaryFormatter();
string path = Application.persistentDataPath + "/player68.test";
FileStream stream = new FileStream(path, FileMode.Create);
PlayerData data = new PlayerData(player);
formatter.Serialize(stream, data);
stream.Close();
}
public static PlayerData LoadPlayer()
{
string path = Application.persistentDataPath + "/player68.test";
IncrementSaveTest player = new IncrementSaveTest();
if (File.Exists(path))
{
BinaryFormatter formatter = new BinaryFormatter();
FileStream stream = new FileStream(path, FileMode.Open);
PlayerData data = formatter.Deserialize(stream) as PlayerData;
stream.Close();
return data;
}
else
{
Debug.LogError("Save file not found in " + path + ". Probably new player");
return null; //return NULL! it will means - no saved data!
}
}
}
and this is your player with initial values:
public class IncrementSaveTest : MonoBehaviour
{
//here you set INITIAL values for new player :)
public float health = 100f;
public float mana = 50f;
public void Start()
{
PlayerData data = SaveSystem2.LoadPlayer();
if(data!=null)
data.Load(this);
}
public void Update()
{
mana += Time.deltaTime;
}
public void OnDestroy()
{ //save data only when its needed -
//by button or when player is destroyed (quit scene)
SaveSystem2.SavePlayer(this);
}
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
onControllerHide() crash 0 Answers
Questing Regarding Save and Load System 0 Answers
Two player touch screen 1 Answer