- Home /
 
Serializing lists
Hey guys, so I've looked around for an answer to my query, and I've found some, but they don't seem to work, I have a class I'm trying to save player profiles with, and then I have a class that's a string list that will contain all the names of the profile files so the game will know the names of what to load.
My list class is in the same C# script as the loading class, but I don't think that matters, it looks like this
 [System.Serializable()]
 
               public class savedProfiles { public List names = new List(); }
public class _GameSaveLoad: MonoBehaviour {
// An example where the encoding can be found is at // http://www.eggheadcafe.com/articles/system.xml.xmlserialization.asp // We will just use the KISS method and cheat a little and use // the examples from the web page since they are fully described
   // This is our local private members
   bool _ShouldSave, _ShouldLoad,_SwitchSave,_SwitchLoad;
   string _FileLocation, fileName;
   string _PlayerName;
    string _data;
 List<UserData> loadedProfiles = new List<UserData>();
 savedProfiles profiles = new savedProfiles();
 
 public List<UserData> LoadedProfiles
 {
     get {return loadedProfiles;}
 }
     
 public int Save(UserData file)
 {
     if (loadedProfiles.Count == null)
     {
         return 1;
         Debug.Log("Profiles list is null or empty");
     }
     else
     {
         
         for (int i = 0; i < loadedProfiles.Count; i ++)
         {
             //finding same named file to replace old file with new
             if (file._iUser.profileName == loadedProfiles[i]._iUser.profileName)
             {
                 loadedProfiles[i] = file;
             }
         }
         
         //Updating the list of profiles
          fileName = "profiles.xml";        
         _data = SerializeObject(profiles);
            CreateXML(fileName);
     
         //Updating each profile
            fileName = "loadedProfiles.xml";        
            _data = SerializeObject(loadedProfiles);
            CreateXML(fileName);
         return 0;
     }
 }
 
 public int NewSave(UserData file)
 {
     for (int i = 0; i < loadedProfiles.Count; i ++)
     {
         //If a profile with the same name already exists, return early with error
         if (file._iUser.profileName == loadedProfiles[i]._iUser.profileName)
         {
             return 1;
         }
     }
     
     loadedProfiles.Add(file);
     profiles.names.Add(file._iUser.profileName);
     _FileLocation=Application.dataPath;
     
     
     //Updating the list of profiles
      fileName = "profiles.xml";        
     _data = SerializeObject(profiles);
        CreateXML(fileName);
     
     //Updating each profile
      fileName = "loadedProfiles.xml";        
     _data = SerializeObject(loadedProfiles);
        CreateXML(fileName);
     
     return 0;
 }
     string UTF8ByteArrayToString(byte[] characters)
 {
       UTF8Encoding encoding = new UTF8Encoding();
       string constructedString = encoding.GetString(characters);
       return (constructedString);
 }
   
 byte[] StringToUTF8ByteArray(string pXmlString)
 {
       UTF8Encoding encoding = new UTF8Encoding();
       byte[] byteArray = encoding.GetBytes(pXmlString);
      return byteArray;
 }
   
 string SerializeObject(object pObject)
 {
       string XmlizedString = null;
     MemoryStream memoryStream = new MemoryStream();
     XmlSerializer xs = new XmlSerializer(typeof(UserData));
        XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
     xs.Serialize(xmlTextWriter, pObject);
       memoryStream = (MemoryStream)xmlTextWriter.BaseStream;
       XmlizedString = UTF8ByteArrayToString(memoryStream.ToArray());
       return XmlizedString;
 }
   
 void CreateXML(string fileName)
 {
     
       StreamWriter writer;
       FileInfo t = new FileInfo(_FileLocation+"\\"+ fileName);
       if(!t.Exists)
       {
          writer = t.CreateText();
       }
       else
       {
         
            t.Delete();
         writer = t.CreateText();
     }
       writer.Write(_data);
       writer.Close();
       Debug.Log("File written.");
 }
 
               but it when I try to use the saving code it states that "the type of arguement object 'saved profiles is not a primitive" what can I do?
Are you trying to save this in playerprefs? Playerprefs doesn't support this kind of thing. You need to use some kind of IO functionality to save the information to a file.
Could you please edit your question to include the relevant parts of your saving code?
as should now be more obvious, I'm not using player prefs as I have a fair amount of data to store, I'm using X$$anonymous$$L files, sorry if there's too much code to make sense of now. For a large part of it I'm using serialization code that someone posted on the unity wiki, but I've also removed and added in functions to better suit what I'm doing.
If I could get any further assistance on this it would be greatly appreciated
Your answer
 
             Follow this Question
Related Questions
Serializing a GameObject, or otherwise saving a scene 1 Answer
Saving a list to json 1 Answer
Storing list aka "save function" 3 Answers
Saving Just Five Top Scores? 1 Answer
How to save list of Sprites? 1 Answer