Lesson 09.1: Creating Traders

Now, we’ll add traders to the game, to let the player buy and sell items.



Lesson Steps


Step 1: Create Engine\Models\Trader.cs

First, we’ll create the Trader class. The Trader will have two properties: Name and Inventory (a List of GameItem objects).

We’ll add two functions: AddItemToInventory and RemoveItemFromInventory, which will be needed for the trader to “buy” and “sell” items.





Step 2: Create Engine\Factories\TraderFactory.cs

Next, we’ll add our factory class, to instantiate Trader objects.

The constructor will create three Trader objects, and add them to the _traders list. The AddTraderToList function will prevent adding a duplicate trader.





Step 3: Modify Engine\Models\Location.cs

The traders will “live” at a location, so we need to add a “TraderHere” property to the Location class. This is on line 19, in the code below.





Step 4: Modify Engine\Factories\WorldFactory.cs

The location’s trader (if it has one) will be populated in the WorldFactory. Do this inside the CreateWorld function, with the other Location creation/population code.

There are three traders. The code for adding them is on lines 20-21, 30-31, and 50-51.





Step 5: Modify Engine\ViewModels\GameSession.cs

We’ll update the GameSession ViewModel class to let the UI know if there is a trader at the player’s current location.

Add a new property CurrentTrader (lines 61-71) and its backing variable “_currentTrader” (line 17).

CurrentTrader will be set when the player moves to a new location. Do this inside the CurrentLocation setter (line 39).

Create a Boolean property “HasTrader” (line 89), to use for the UI to decide whether or not to display the “Trade” button – based on whether or not there is a CurrentTrader.





Step 6: Modify WPFUI\MainWindow.xaml

Finally, add a “Trade” button to the UI (lines 264-267). Its Visibility will be based on the Boolean HasTrader property in GameSession.cs.




Step 7: Test the game

Run the game and move to the locations that should have a trader (the Farmer’s House, the Trading Shop, and the Herbalist’s Hut). You should see the “Trade” button at those locations, and not see it at the other locations.


Return to main page

2 thoughts on “Lesson 09.1: Creating Traders

  1. I think anyone who’s followed along this far would catch it anyway, but you might want to add an annotation or something to the video to remind people to add “CurrentTrader = CurrentLocation.TraderHere;” to the CurrentLocation setter in the GameSession class since it seems you didn’t mention it or show it off in your explanation. It’s clearly mentioned in the text and is an obvious thing that needs to happen, but might save people a little effort when they follow your video’s instructions and find it still doesn’t work 🙂

    1. Thanks for mentioning that Danielle. Unfortunately, it looks like YouTube removed the ability to add annotations last June. I’ll see if there is any way to correct that – without making a corrected video.

Leave a Reply

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