Custom Class to String Array? Is it possible?

Hello, I’m running into an issue with my object oriented building and I need to save the data for my game. I have come up with a way to save my data through classes but I need to convert the large data to a string[ ].

Sample to get the jist of what I have.

	void Save () {     /* Create a new Save */
		SaveData myData = new SaveData ();
		string[] mainData = {myData.lives.ToString(),
			myData.tokens.ToString(),
			myData.worldsUnlocked.ToString()};
		string[] world1Data = {myData.world1.levelsCleared.ToString()};
		
		/* Though it wasn't efficient to do it this way */
	}
	
	public class SaveData { /* Main Game Data */
		public int lives = 5;
		public int currency = 0;
		public int worldsUnlocked = 0;
		public WorldData world1 = new WorldData();
		public WorldData world2 = new WorldData();
		public WorldData world3 = new WorldData();
	}
	
	public class WorldData { /* Save Data for every World*/
		public int levelsUnlocked = 0;
		public LevelData level1 = new LevelData();
		public LevelData level2 = new LevelData();
		public LevelData level3 = new LevelData();
		public LevelData level4 = new LevelData();
		public LevelData level5 = new LevelData();
	}
	
	public class LevelData { /* Save Data for every Level*/
		public int highScore = 0;

	}

Why do you want to save it into a string, arent there any other ways you can take? There are plenty of free save/serialization assets out there.

If you really want to use strings, you can take serialize your data into JSON or XML and use this as a string. (There are assets and tutorials out there for this.)

I thought there were and I tend to stay away from third party assets just a personal thing. The script was save the file as a custom extension onto the HD and load it from the HD. The script prototype worked. But when coding this I wondered if there was a more efficient way then to create a lot of string arrays and such. I might use XML, serialization is a somewhat new concept to me.

Serialization is a automated process to transform data to a single file, using binary or XML way.
You’ll be able to serialize/deserialize very easily without any painful String conversion.

If I’m correct then I was probably doing it through binary but in a more complex way; as the prototype took the data, organized into a string array and I used File.WriteAllLines into a ‘fileName.xxx’ and the loader would read the lines in the same order? Also what’s the difference between Binary and XML and when does benefit to use the other in certain situations?

Thanks for the explanation, I feel stupid now. I tend to avoid plugins without knowing the process(es) myself and I try them out myself. XML seems more common from what I’ve seen.

Depends on the platform you’re targeting. XML can be very troublesome on some platforms and has limited support on some as well. You could look at something like my JSON .NET conversion:

http://u3d.as/5q2

I’m getting ready to push an update which includes full WinRT and Windows Phone 8 support as well as a couple Web Player fixes and a better simplified folder structure. It supports both JSON and BSON (binary). Using binary (such as BSON) makes your files a little more difficult to tamper with. It’s also more compact for such things as pushing data to a website / service.

Personally I prefer JSON for my data serialization. It’s lightweight and standardized, so you can rely on it. JSON .NET is a great solution, but if you’re looking for something more lightweight you can also use MiniJSON.

As far as I know, it’s going to be a PC only game so that’s the platform.

Yeah if it’s going to be PC only you could get by with something like miniJSON. Or, you could just use the official pre-compiled JSON .NET if you wanted something more robust. No need to use my asset as it’s built to add support for other platforms.

http://json.codeplex.com