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.

 

Trader.cs

 

 

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.

 

TraderFactory.cs

 

 

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.

 

Location.cs

 

 

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.

 

WorldFactory.cs

 

 

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.

 

GameSession.cs

 

 

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.

 

MainWindow.xaml

 

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

4 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.

  2. When I first implemented the change in Gamesession, “CurrentTrader = CurrentLocation.TraderHere;” I accidentally did “_currentTrader = CurrentLocation.TraderHere;”. As a result I was unable to see the trade button at the different trade locations. I eventually was able to spot the difference between my code and yours, but I am having a hard time see why CurrentLocation.TraderHere wasn’t able to set the _currentTrader variable directly? I stepped through using the debugging and the _currentTrader variable does get set to a trader at a given location, e.g. susan at the trader shop, pete at the herb hut. Yet, I was unable to see the trade button in the UI until I made the correction I mentioned previously. Any thoughts? FYI, Added the link to my github repo of the commit that had the issue in the website dialog. Thanks!

    1. Hi Robert,

      If I’m reading the code differentials correctly (I’m not a GitHub expert), I think this is the source of the problem.

      Directly setting the “_currentTrader” backing variable, instead of going through CurrentTrader’s “get”, means “OnPropertyChanged(nameof(HasTrader));” is never run (line 70 of GameSession.cs). The button’s Visibility is bound to the HasTrader property in the XAML. And, since it never receives a properychanged notification, it never updates the Visbility.

      Let me know if that isn’t clear, or if I’m misunderstanding something.

Leave a Reply

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