Saving Works! Now to Load It Again...

Hey Folks! Got the save system working today, and have written out my first few save files successfully. They're doozies, too. 700kB of text data just for that cramped ship layout you see above with two crew members. Lots of stuff to save, I guess. Star system info, every object in the game, the ships, waypoints, message logs...the works. It'll be interesting to see how this pans out with larger/longer games.

There was a minor hang-up involving litJson and some Unity Vector2s. Apparently, one of the properties of a Unity Vector2 is "normalized," which is another Vector2. And when you're trying to serialize an object that contains an object of the same type...infinite recursion! I briefly considered switching to Unity's built-in Json handler until I remembered it doesn't support dictionaries, which I use liberally. So I had to add some code to litJson to handle these and a few other special Unity types to hopefully avoid that in the future.

In the meantime, I still need to get this thing loaded in again, and that's where today's work was mostly focused.

One of the first major hurdles was clearing the current scene before loading, in case a game was already in progress. That means Destroy() functions. I'm not 100% certain this is necessary with C#/Unity garbage collection, but to be on the safe side, I added my own clean-up code to each class and called it manually. I have a ton of custom classes in the game which don't follow the "Unity way," so this seemed prudent.

Once I was able to clean-up the current scene, it was time to start loading the data from file.

Pretty early on, I noticed I already made a mistake. I store all the condition owners (i.e. objects that can have stats, like people and ship parts. COs for short) at the star system level, but I tend to need this info while I'm initializing a ship. I thought I could be clever by having the star system just list all the COs and when the ship needed one during loading, it grabbed the data from there.

However, that's starting to look like a lot of data being thrown around between classes. So what I'm thinking instead is that I'll have the star system load all the COs first, then maybe have the star system add relevant COs to each ship before the ship gets populated. And later, when ships are being populated, they already have the COs and just have to grab them from that list.

In theory, anyway. We'll see if that still makes sense in the morning :)

Tags: Ostranauts


matsy's picture

In the past using Json I've seen that setting the formatting to none makes a big difference to file size. Makes the file near enough unreadable but some text viewers reformat it automatically when opened so not the end of the world.

dcfedor's picture

Does that omit line breaks and tabs? I wonder if that's called something else in this lib. "Pretty" comes to mind.

I'm thinking I might have to do some optimization in the future, just given the sheer amount of chaff in that file. Things like verbose per-item history logs, GUIDs that probably could be shorter, etc.

Dan Fedor - Founder, Blue Bottle Games