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

Leave a Reply

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