Hello all,
I am pretty new to all this (I have been saying that for the last 6 months… so am I still new?)
I am currently creating a Save and Load system (using binary stream, not integrated PlayerPrefs module).
here is the situation :
it is a puzzle game, I have several Levels. In each level, let’s say that there are two variables coming out : int score, and bool clear.
I followed Brackeys’ tutorial on Save&Load System
It works fine, but when you want to keep each Level’s variable intact, to give the ability to the player to come back again later on to acquire a higher score, the system doesn’t keep the specified level’s score and bool, but onlyI have only saves the last played level.
I made a chart to make it more visual :
you have 3 levels on the left. the last played level is level 3.
the player saves the game and quits
he comes back later on and wants to try level2 again.
but the current script has only kept level 3’s variable.
How can I save each level’s variables intact so that it is reusable later on?
make a List or an array of levels?
Make a new Save And Load System separately for each level each time?
what is your suggestion?
ok, I have been trying so bad to adjust the codes from Brackey’s tutorial, but it seems I don’t have a sufficient coding level to solve this.
here is what I did :
1: made TGameDat class, serialized:
[System.Serializable]
public class TGameDat
{
public int tGameDatInt;
public bool tGameDatBool;
public int tSceneIndex;
public TGameDat (TPlayer player)
{
tGameDatInt = player.tInt;
tGameDatBool = player.tBool;
tSceneIndex = player.tScene;
}
}
2: made a TSave class to save and load the game:
the important part of this code is to make a List of the scores for each Level/Scene, to come back later and play again
public static class TSave
{
public static void TSavePlayer (TPlayer player)
{
BinaryFormatter formatter = new BinaryFormatter();
string path = Application.persistentDataPath + "/Tsave.fun";
if(File.Exists(path))
{
FileStream stream = new FileStream(path, FileMode.Open);
List<TGameDat> data = new List<TGameDat>();
data.Add(new TGameDat(player));
formatter.Serialize(stream, data);
stream.Close();
}
else
{
FileStream stream = new FileStream(path, FileMode.Create);
List<TGameDat> data = new List<TGameDat>();
data.Add(new TGameDat(player));
formatter.Serialize(stream, data);
stream.Close();
}
public static List<TGameDat> TLoadPlayer()
{
string path = Application.persistentDataPath + "/Tsave.fun";
if(File.Exists(path))
{
BinaryFormatter formatter = new BinaryFormatter();
FileStream stream = File.Open(path, FileMode.Open);
List<TGameDat> data = new List<TGameDat>();
data = formatter.Deserialize(stream) as List<TGameDat>;
stream.Close();
return data;
}
else
{
Debug.LogError("Save file not found in " + path);
return null;
}
}
3: Finally, TPlayer derived from Mono, attached to a GameObject on the scene.
public class TPlayer : MonoBehaviour
{
public int tInt = 0;
public bool tBool = false;
public int tScene;
public void TSceneMaker()
{
tScene = SceneManager.GetActiveScene().buildIndex;
}
public void TSaveVariables()
{
TSave.TSavePlayer(this);
Debug.Log("saved");
Debug.Log(tInt + " " + tBool + " " + tScene);
}
public void TLoadVariables()
{
List<TGameDat> data = TSave.TLoadPlayer();
Debug.Log("started to load?");
for (int i = 0; i < data.Count; i++)
{
if(data.tSceneIndex == tScene)
{
tInt = data.tGameDatInt;
tBool = data.tGameDatBool;
tScene = data.tSceneIndex;
Debug.Log("loaded. data count = " + data.Count);
}
else
{
Debug.Log("level has not been saved");
}
}
}
The problem is : when I go back and forth to different levels, the data has not been saved, and the List counts always only 1, although I want it to be cumulative. What am I missing? Thank you for you help!