Jump to content
  • Advertisement
Sign in to follow this  
noodleBowl

Saving Object data

This topic is 1123 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm starting to look into how I should save my game state and 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

Share this post


Link to post
Share on other sites
Advertisement

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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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?

Edited by noodleBowl

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!