Lesson 08.1: Completing Quests

In this lesson, we’re going to check if the player can complete any quests when they move to a location with quests. If they, we’ll remove the quest completion items from their inventory and give them the quest rewards.




Lesson Steps


Step 1: Modify Engine\Models\Player.cs

To complete a quest, the player must turn in some items. So, we will need to remove items from the player’s inventory.

We want to do this through a function, just like we do when we add items to the player’s inventory. This lets us notify the UI of the changes.

I’ve added a new function for this, RemoveItemsFromInventory (on lines 100 through 105).

We also need a function to check if the player has all the items required to complete the quest. The new function HasAllTheseItems (on lines 107 through 118) accepts a list of ItemQuantity objects and looks through the player’s inventory.

If the count of items is less than the number required in the parameter, the function returns “false” – the player does not have all the items. If the player has a large enough quantity, for all the items passed into the function, it will return “true”.





Step 2: Modify Engine\ViewModels\GamesSession.cs

First, we will let the player know when they receive a new quest, and what the details of the quest are.

Inside the GivePlayerQuestsAtLocation function (line 176), we’ll raise messages (for the UI to display) letting the player know the name and description of the quest, the items they need to return with, and the rewards they will receive when they complete the quest.


Second, we’ll modify the CurrentLocation setter. On line 34, I’ve added a call to a new function – CompleteQuestsAtLocation(). So, when the player moves to a new location, this function will be called, and we will see if the player can complete the quests at that location.


Finally, we’ll write the code for CompletQuestsAtLocation (lines 130 through 174).

We loop through all the quests available at this location, starting on line 132. If this location does not have any quests, we will loop zero times.

On lines 134 and 135, we get the first quest from the player’s Quests property where the quest ID matches the location’s quest’s ID, and the player has not already completed.

If the player does not have a quest that matches this criteria, “questToComplete” will be null.

If “questToComplete” is not null (the player has this quest, and has not completed it), we check if the player has all the items needed to complete the quest (the “if” section, starting on line 140).

If the player has all the quest completion items, we remove them from the player’s inventory (lines 143 through 149). Then, we give the player the rewards for completing the quest (lines 155 through 167).

Finally, on line 170, we mark the quest as completed.







Step 3: Run the program, and test the changes.



Return to main page

Leave a Reply

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