Reputation: 79
I'm trying to figure out how you would save & load list of data. I've gotten my code to work with single data but unsure of how a list one would work. I have a Character class so that means I can have multiple characters and I would like to save each of those character's hp, mana etc.
//SaveManager class that I can call from anywhere to save and load
public static class SaveManager
{
//SAVE
public static void Save(Character c)
{
BinaryFormatter formatter = new BinaryFormatter();
string path = Application.persistentDataPath + "/Save.save";
FileStream stream = new FileStream(path, FileMode.Create);
PartyData data = new PartyData(c);
formatter.Serialize(stream, data);
stream.Close();
}
//LOAD
public static PartyData Load()
{
string path = Application.persistentDataPath + "/Save.save";
if (File.Exists(path))
{
BinaryFormatter formatter = new BinaryFormatter();
FileStream stream = new FileStream(path, FileMode.Open);
PartyData data = formatter.Deserialize(stream) as PartyData;
stream.Close();
return data;
}
else
{
Debug.Log("Save FIle not Found");
return null;
}
}
}
PartyData class is what I'm using to save the Character data to.
[System.Serializable]
public class PartyData
{
public int hp;
public PartyData(Character cParty)
{
hp = cParty.HP;
}
}
Character class that contains stats etc
public class Character
{
public int HP { get; set; }
public int Mana { get; set; }
}
And then finally I have the CharacterParty class that I attach to a gameobject, this class contains multiple characters and calls the Save and Load functions:
public class CharacterParty : MonoBehaviour
{
[SerializeField] List<Character> characters;
public List<Character> Characters
{
get
{
return characters;
}
}
public void SaveParty()
{
SaveManager.SaveMC(characters[0]);
}
public void LoadParty()
{
PartyData data = SaveManager.LoadMC();
characters[0].HP = data.hp;
}
}
Now for testing purposes I tried saving & loading just the character's hp at index 0 and it works but now I wanna save a list of multiple character's hp and mana etc. I just don't know how the list would work with serialization saving and loading. My code probably needs a little bit of change to get the list working so I hope someone can help me out with an example.
Upvotes: 1
Views: 7102
Reputation: 38
Use JsonUtility class - this is Unity's built-in utility for JSON.
Also with this you can serialize any class without [System.Serializable]
Save:
// Parties list
List<PartyData> parties = new List<PartyData>();
// Add your parties here
// First argument is an instance of a class or any other object
// If second argument set to true, it makes JSON more human-readable
string json = JsonUtility.ToJson(parties, false);
// .. saving the file
Load:
// .. loading the file
// First argument is the JSON
List<PartyData> parties = JsonUtility.FromJson<List<PartyData>>(json);
// .. do stuff with the list
EDIT:
If you really want to use BinaryFormatter, then this is for saving:
List<PartyData> parties = new List<PartyData>();
// .. add parties
formatter.Serialize(stream, parties)
For loading:
List<PartyData> parties = formatter.Deserialize(stream) as List<PartyData>;
// .. do stuff
SECOND EDIT:
This one should work. You would do something like that:
// Save class
[Serializable]
public class Save
{
public List<PartyData> parties;
// .. add other stuff here
}
// For loading
try
{
// Deserializing the save
Save save = (Save)formatter.Deserialize(stream);
// .. do stuff with other data if you added some
foreach (PartyData data in save.parties)
{
// .. do stuff with data
}
}
catch (Exception e)
{
// .. can you just acnowledge... E
// Oopsie! An error occured. The save file is probably COWWUPTED
Debug.LogWarning($"An error occured while loading the file: {e}");
}
// For saving
List<PartyData> parties = new List<PartyData>();
// .. add parties by calling parties.Add(party_here);
// Creating a save
Save save = new Save();
// Adding parties to save
save.parties = parties;
// Serialize to a file
formatter.Serialize(stream, parties);
Upvotes: 2