Lesson 14.5: Move Remaining Game Data to XML Files

Let’s finish converting the factory classes to create objects from XML files.




Lesson Steps

Step 1: Create the new XML files.

In the Engine project, in the GameData folder, create three new XML files: Quests.xml, Recipes.xml, and Traders.xml.

Notice that the Traders have an ID in their XML data. We’re going to use that later.

Set their “Build Action” to “None” and “Copy to Output Directory” to “Copy Always”.








Step 2: Change Engine\Models\Trader.cs

To be consistent with our other classes, I added an ID property to the Trader class. This will let us access the traders by ID, instead of Name – like we do for all our other model objects.




Step 3: Modify Engine\Factories\QuestFactory.cs, RecipeFactory.cs, and TraderFactory.cs.

Change these to load from the XML files, like we did with the user factory classes.








Step 3: Modify Engine\GameData\Locations.xml

For locations that have a Trader, change the Trader node to have an ID attribute, instead of using a Name attribute – since the TraderFactory now gets Traders by ID.

Change lines 11, 18, and 37 to store the Trader’s ID, instead of their Name.




Step 4: Modify Engine\Factories\WorldFactory.cs

Change line 97 to get the location’s Trader with the new ID attribute, and the TraderFactory’s new GetTraderByID function.





Return to main page

2 thoughts on “Lesson 14.5: Move Remaining Game Data to XML Files

  1. Hi Scott,

    Thank you for putting the time and work in to bring us such informative tutorials! I have one question about the project now that I have gone through everything.

    What is involved in saving the state of the game? For example, if I were to level up to level 3 and close the game, I would want to be able to open it back up later and still be level 3. Obviously right now, everything resets and goes back to the beginning states and values.

    1. Hi John,

      You’re welcome. In lesson 19.4 of the Windows Form version of this game, I added code to save the Player object’s information to disk, and load it when you restart the game.

      You could add the same type of logic to this game – making whatever changes you need to make to match this version’s Player class. It might be even easier to look at JSON serialization, and maybe use the Newtonsoft.JSON library to do JSON serialization. In lesson 19.4, we manually do XML serialization. If you can have a library automatically do that for you, that’s better.

Leave a Reply

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