- Home /
The question is answered, right answer was accepted
(SOLVED!) Saving/loading data for EACH LEVEL with MULTIPLE VARIABLES
Hi guys! A basic question about saving/loading data for different levels, that I can not find anywhere!
I basically want to save the results (multiple variables such as the "score", "time", etc.) of EACH level in a file.
For that, I used a class whit all that variables inside and then create a new copy of that class, naming them with the different level numbers. Is there any other way where I can access my data without writing 1000 lines for each variable and each level? Here's a short example of what I am intending:
public class playerScore
{
public int score;
public float time;
public bool levelUnlocked;
}
//Save Method
void Save(){
if (levelNum == 1){
playerScore scoreLev1 = new playerScore ();
bf.Serialize (file, scoreLev1);
}
if (levelNum == 2){
playerScore scoreLev2 = new playerScore ();
bf.Serialize (file, scoreLev2);
}
//if (levelNum == 3)
//etc...
}
//Load Method
void Load(){
if (levelNum == 1){
score = scoreLev1.score;
time = scoreLev1.time;
levelUnlocked = scoreLev1.levelUnlocked;
}
if (levelNum == 2){
//etc...
}
Can someone tell me how to do it following the example above? Or is there any better way? Thank you so much in advance :)
Have a custom class that holds the data for each level:
[System.Serializalbe]//attribute for viewing and editing it in the inspector
public class LevelData {
public int score;
public float time;
}
Have an array or list or other type of collection with the class type as the element type and then you can access each level data by simply using the level number as the array index.
public LevelData[] levelData_array = new LevelData[5];//holds data for 5 levels
//create a new LevelData instance at position 2 of the array
levelData_array[2] = new LevelData() {
score = 34;
time = 186.45f;
};
//print the time of level 2 to the cconsole
Debug.Log(levelData_array[2].time);
//change the score value of level 2
levelData_array[2].score = 0;
Remember that arrays start at index 0.
Since the LevelData has only basic variable Types like ints and floats, there should be no problems serializing single LevelData instances or even the whole levelData_array array.
Answer by fafase · Aug 20, 2017 at 01:54 PM
Instead of saving each level, save them as a list.
Get the file, open it, deserialize and you now have a list of playerScore. Then append the new score (or update the existing ones).
Serialize again and done.
Now what does that simplify? First off, the length of your list tells right away whether a level is unlocked. If you have a list of 20 items, then it means level 21 has not been saved yet. Make sure to keep them in order so you can retrieve any level with the list index for update.
void AddNewItemToList(parameter list){
BinaryFormatter bf = new BinaryFormatter();
MemoryStream ms = new MemoryStream(Convert.FromBase64String(str));
List<PlayerScore> ps = bf.Deserialize(ms) as List<PlayerScore>;
ps.Add(new PlayerScore(parameter list));
ms = new MemoryStream();
bf.Serialize(ms, ps as List<PlayerScore>);
SaveDocumentOnDrive();
}
List<PlayerScore> GetPlayerScore(){
BinaryFormatter bf = new BinaryFormatter();
MemoryStream ms = new MemoryStream(Convert.FromBase64String(str));
return bf.Deserialize(ms) as List<PlayerScore>;
}
Answer by maymo · Aug 20, 2017 at 03:15 PM
Thank you so much guys for your quick, perfectly explained and clear answers! That's exacly what I needed it. I'll use an array to do it, as I can understand it better than a List.
Follow this Question
Related Questions
One scene, many levels?? 2 Answers
Save quantities 1 Answer
Need a clue. The best way to save level data without duplication 0 Answers
Multilevel shooter? 2 Answers
Attaching multiple objects to one object with script. 1 Answer