Periodic: Dev Log 9

Hi Everyone! I've got another update, and while this isn't a huge update, it took a lot more time to get working properly than I had originally thought.

TL:DR - Save system can now merge in from the template if there is an update, lots of little bug fixes

Below is the full story:

Update - This update was a challenging one, mostly because I had to fight a little bit with the LitJSON library to have it do what I wanted.

The trick was to get the LitJSON object to merge in my save template with the current save file for when I do updates to the game and add new fields. If the field doesn't exist in the save object and I had tried to access it, the game would throw an error and things wouldn't work right.

This new part of the save system allows me to edit my save template, and when an update of the game goes out, if the player has a save file this will update it to the new version. Since it doesn't hurt to have fields exist in the save file, I can leave things in the save that aren't in the template, but not vice-versa.

I'm going to put the code for that I'll be talking about above the description so you can follow along:

First we have MergeTemplateWithSaveFile(), and what this does is start the process. I read in a copy of the save template, and at the highest level using the JsonData object (LitJSON specific) I can pull all of the keys at the level I'd like. Since we're at the root of the object, I'm pulling all of the keys within the first set of curly braces. Next, using a foreach loop, I'm pulling the key out and passing it in to my MergeLayer function that takes the key, and both JsonData objects at the level I want to parse through the keys.

Once inside MergeLayer(), I do a quick check to see if the key is an object (new set of curly braces) or an array (square brackets) since those are the only ones that can contain children. If that's true, I enable the hasKeys flag and keep moving.
The next part will check the save file's JSON, and if it's missing the object, it will assign it properly.
From there, I get into what was the most frustrating part of this, and that was recursively going down another layer with MergeLayer(). For those who don't know what recursion is, the simplest explanation is calling a function from itself, and eventually working your way back up. The tricky part here was handling if the object was an array. All that was needed to get the next layer to work with the current key was to access the JsonData object using the key and pass it down. This works because C# passes everything by reference, meaning the JsonData that's passed is the SAME object once it goes into the next function, and changing it changes it in the calling code.
For the array, I had to loop through the array elements by index, and then pass the subKey I wanted to check/modify. Lots of work, but when it runs it seems like it goes really quick for a ~3Mb save template.

What's Next - To finish the save system, I'll need to make sure making changes in the save file is working, as well as adding new items to the objects in the save file based on events. From there, I had /u/CybixStudios play through the game and give some feedback on the controls, so that's going to be getting updated before I start working on the layout of the next level!

That should do it for this update, and I'll catch ya next time!

-TJ