Setting up Shop! Creating a shop in a mobile adventure game (3): Updating the Player’s Inventory

Marcus Ansley
3 min readOct 9, 2021

--

As mentioned in the previous article, I’m opting for a simpler inventory system than what we might use for a more extensive game than the one I’m working on here. For that, what I’d personally recommend is taking a look into setting up some sort of dedicated PlayerInventory class which our Player class has direct access to. Particularly, a ScriptableObject would be a good choice for this 😉

For our purposes here, I just want the inventory to do two things: 1) store the information on the Items collected, 2) store the quantity of those Items in the player’s possession. As such, a Dictionary (a container of key-value pairs) offers us a quick and easy solution to this, allowing us to store the Item as the key and the quantity as the value. One of the major benefits to using a Dictionary (aside from these key-value pairs) is that, unlike a List or an Array for instance, we don’t need to iterate through it just to find a single element but can instead retrieve it via an index. To declare and initialise this Dictionary, we only need one line of code:

Our player’s inventory is now ready and available for other classes to access. Let’s hop back over to our ShopKeeper class and take a look at that UpdatePlayerInventory method now:

This method first tries to assess whether the purchased item already exists within the player’s inventory by calling the Dictionary.TryGetValue method (which passes in the key (the item struct itself) and returns the key if successful (the integer quantity value)). If it’s successful, we can then access this element within the dictionary by using the purchased item as the index, then assigning the value “++quantity” to it (increasing the number of the item stored in the Dictionary, at least for our reference). To use the fancy, proper terminology for this, we’re assigning an integer value with a pre-increment unary operator, since this “++” only needs one operand and comes before the operand. This is quite important, because if we wrote “quantity++” in this case we would in fact be assigning the current quantity, then incrementing the variable, then letting the variable fall out of scope (meaning our quantity key would never actually get increased).

In the event the player doesn’t already have the item, all we need to do is call “_player._playerInventory.Add(item, 1)” to add it as a new element to the Dictionary with a value of 1 (for our int quantity).

Lastly, as a more specific bit of logic in this game, we want to keep track of whenever the player obtains the KeyToCastle item (as this will allow them to progress to a new area). As such, we’re just checking to see if the item’s ItemType is equal to ItemType.KeyToCastle, before changing the value of a public boolean property on the Player class for us to easily check whether it’s been collected.

With this done, we finally have our working shop and purchasing system up and running in our game! 😄

The working system, as shown in the first of these three articles 😉

--

--