Saving Object data
My initial thoughts were to make objects serializable and just save them to the disk. Although I'm not sure if this is the best approach since I have heard object serialization is slow on android (not only for saving, but for loading aswell)
I also thought about converting my objects into a JSON string (this is my preferred way), but I'm kind of lost on how to actually convert the object to a JSON string and vice versa
Thoughts on what I should do? Any help would be appreciated
Can be slow, yes, depending on how much you are writing and the formats you are using.
The more parsing you do, the slower it will be. json is a plain text format with many characters that must be escaped, which means you will need a lot of serialization work.
Even though it is less verbose than markup languages like XML, json is still quite large. Storing a single integer, instead of being 4 bytes, can be 11 bytes plus the size of your label and the surrounding blocks. Same thing with floating point values, you'll probably be at 15 bytes of text to write the 4 byte value. Add on the tag, the quotes and commas and colons, and you're looking around 20 to 25 bytes to tag and store 4 bytes of data.
So you start with 5x more data than necessary, and to parse it you must perform many conversions to get from text to value, then again from tagged values into a data structure.
There are much more efficient ways to store binary data, and in some situations you can store it so no decoding is required.
Thoughts on what I should do?
Yes. Read some of the volumes that have been written about efficient serialization routines, efficiently packing data for decoding, about asynchronous IO, and methods for streaming in resources quickly.
You could use SimpleJson to convert your objects into Json strings and vice versa automatically.
Look at the Facebook project on Github here: github.com/facebook-csharp-sdk/simple-json/
Be aware that SimpleJson has some limitation when it comes to C# Enums and can't deserialize them correctly after serialising them. I've made a fork of it and have applied a patch, you can find that here:
https://github.com/gameup-io/gameup-unity-sdk/blob/master/SimpleJson.cs#L21
In terms of performance, we've not had many problems with it as long as the string/object is not huge.
Hope this helps,
Mo
Mo, I'm curious why you made the jump to Unity and C#, when neither of those are mentioned in the post, and Noodlebowl hasn't talked about Unity or C# in any of his recent posts or comments.
My recommendation remains that you use a save data format that is more information-dense than json, that requires far less processing and less space. There are many well-documented alternatives, such as tagged binary data and direct-from-memory versioned data structures.
My recommendation remains that you use a save data format that is more information-dense than json, that requires far less processing and less space. There are many well-documented alternatives, such as tagged binary data and direct-from-memory versioned data structures.
So lets say for example I use tagged binary data and I have a data structure like:
public class Monster
{
public int id;
public String name;
public int health;
}
//Init
Monster mon = new Monster();
mon.id = 1;
mon.name = "goblin";
mon.health = 10;
I would write everything out to a file (Not too sure about that class tag):
//Example writing to flie
//Class Name
out.writeUTF(mon.getClass().getName());
out.writeUTF("Class");
out.writeUTF(mon.getClass().getSimplifiedName());
//First field
Fields [] fields = mon.getClass().getDeclaredFields();
out.writeUTF(fields[0].getName());
out.writeUTF(fields[0].getType().getName());
out.writeUTF(fields[0].get(mon));
//File contents (Would be in bytes):
[com$Enemies$Monster.Class.Monster][id.int.1][name.String.goblin][health.int.10]
Is this about right?
Also what about future proofing? Lets say I add a bunch of stuff later on like spell power, a list of spells, defense, and etc.
Normally I would think that this would destroy my save, because the data would have to be packed in a specific way. I guess with TAG this is circumvented, because it is TAG? Is this assumption right?