Lesson 17.2: Reducing serialized data in save game file

Now that we added the code to save and load a game, let’s see if we can improve it.

The first thing I’d like to improve is the size of the save game file. We serialize all the objects’ properties, but only use a few of them.

So, let’s add an attribute to the properties we want the SerializeObject function to ignore.

 

 

 

Lesson Steps

 

Step 1: Modify \Engine\Models\GameItem.cs

The only property we need from this class is the ItemTypeID. We pass that value to the factory and it creates the GameItem object, with all the other values.

So, we’ll add the new “using Newtonsoft.Json;” directive on line 2 (so we can use classes from the Newtonsoft.Json NuGet package.

Then, we’ll add a “[JsonIgnore]” attribute before all the properties we don’t need – all of them except ItemTypeID. This way, when we call JsonConvert.SerializeObject in SaveGameService, those properties will not be in the string that is created.

This is what we’ll do for the other classes in this lesson.

 

GameItem.cs

 

 

Step 2: Modify \Engine\Models\Inventory.cs

Add “using Newtonsoft.Json;” and put a “[JsonIgnore]” attribute before: GroupedInventoryItems, Weapons, Consumables, and HasConsumable. All those properties will be populated when we add objects to the Items property.

 

Inventory.cs

 

 

Step 3: Modify \Engine\Models\LivingEntity.cs

Add “using Newtonsoft.Json;” and put a “[JsonIgnore]” attribute before: IsAlive and IsDead.

 

LivingEntity.cs

 

 

Step 4: Modify \Engine\Models\Location.cs

Add “using Newtonsoft.Json;” and put a “[JsonIgnore]” attribute before: Name, Description, ImageName, QuestsAvailableHere, MonstersHere, and TraderHere.

 

Location.cs

 

 

Step 5: Modify \Engine\Models\Quest.cs

Add “using Newtonsoft.Json;” and put a “[JsonIgnore]” attribute before: Name, Description, ItemsToComplete, RewardExperiencePoints, RewardGold, RewardItems, and ToolTipContents.

 

Quest.cs

 

 

Step 6: Modify \Engine\Models\Recipe.cs

Add “using Newtonsoft.Json;” and put a “[JsonIgnore]” attribute before: Name, Ingrdients, OutputItems, and ToolTipContents.

 

Recipe.cs

 

 

Step 7: Modify \Engine\Models\GameSession.cs

Add “using Newtonsoft.Json;” and put a “[JsonIgnore]” attribute before: CurrentWorld, CurrentMonster, CurrentTrader, HasLocationToNorth, HasLocationToEast, HasLocationToSouth, HasLocationToWest, HasMonster, and HasTrader.

 

GameSession.cs

 

 

Step 8: Test the changes

Open the game, move around, then exit the game. Re-open the game and check that the player is in the last location.

Even though we changed the format of the save game file, we don’t need to give its format a new version number. This is because we didn’t change any of the properties that were used in our function that deserializes the save game data.

 

 

Additional links for this project

Source code: https://github.com/ScottLilly/SOSCSRPG

Project plan: https://github.com/ScottLilly/SOSCSRPG/projects/1

Discord: https://discord.gg/AUYXYtH

Return to main page

Leave a Reply

Your email address will not be published. Required fields are marked *