Lesson 99.1 – Preventing duplicate quests

A student reported an unusual bug in the game, and this lesson will cover how to fix it.

 

Bug Description

This bug happens if you are storing the saved game data in a SQL database – using the PlayerDataMapper.

If the player ends the game when they are at a location that has a quest, there will be an error the next time the player tries to start the game.

 

Bug Analysis

PlayerDataMapper.CreateFromDatabase reads the data from the SavedGame table first. This includes the value for the player’s current location.

On line 55, we were creating the Player object by calling Player.CreatePlayerFromDatabase, and passing in the CurrentLocation ID. After creating the Player object, we called the MoveTo() function, to set the player in their last location.

The MoveTo function noticed there was a quest at the location and gave the quest to the player.

Later, in PlayerDataMapper.CreateFromDatabase, we read the player’s quests from the Quest table, and add those quests to their quest list.

The bug happened because we were adding the same quest to the player’s quest list – once from moving to their last location, and once from the database list of quests they already had.

The duplicate quest caused the game to crash.

 

Bug Fix

To fix this, I commented out line 158 of Player.cs – the line moving the player to the location.

Next, in PlayerDataMapper.cs, I moved the declaration of the currentLocationID variable from line 52 to line 22. This is so it would be visible outside the “using” section from line 25 through 59 (in the original code).

Finally, on line 134, I set the player object’s CurrentLocation property. This is after we added the player’s quests from the database.

 

FIXED CODE

 

Player.cs

 

 

PlayerDataMapper.cs

 

 

If you have any problems applying this change, or notice any other bugs, please leave a comment below.

Leave a Reply

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