Setting up Shop! Creating a shop in a mobile adventure game (2): The Shopkeeper

Carrying on with our work on the mobile adventure game’s shop system, let’s take a look at the ShopKeeper class 😉 In particular, there are three key methods to this script: SelectItem, AttemptPurchase, and UpdatePlayerInventory.

SelectItem

As we saw in the previous article, this public method gets called from one of the three PurchaseOption objects whenever the player makes a selection (but not a purchase just yet). We send the item’s information to the ShopKeeper by passing in that struct, along with a separate “shopID” integer. Let’s take a look at the method:

We change the local selectedItem variable (of type Item) by assigning “item” to it, and then use the shopID within a switch statement to call a method on the UIManager. All that method does is reposition a blue highlight to show which item is currently selected. In code, we’re just changing the Image’s rectTransform.anchoredPosition (which’ll move it in relation to its current parent, making it easier for us to find the desired coordinates by manually repositioning it):

And in game, that looks like this:

With this all done, our BuyItem button’s all ready for the player purchase the selectedItem. Speaking of which, let’ take a look at the public “AttemptPurchase” method:

AttemptPurchase

Ultimately, this method checks to see whether the players gem-count is greater-than or equal-to the selectedItem’s value (i.e. its price). If it isn’t, then we currently just close the shop by calling the UIManager.CloseShop method, but I’m eventually hoping to implement some dialogue for the ShopKeeper before this happens. If the player does have enough gems, however then we subtract the selectedItem’s value from their gem-count, call the UpdatePlayerInventory method (passing in the selectedItem), and finally update the gem displays via the UIManager’s UpdateGemsDisplay method.

The ShopKeeper.AttemptPurchase method

Since we’re here, let’s take a brief look at that UpdateGemsDisplay method in the UIManager:

This method updates both the _gemsText_main (a TextMeshProUGUI object; the one which remains part of the players HUD or “Heads-up Display”) and the _gemsText_shop (another TextMeshProUGUI object, but specifically part of the shop’s UI). As a matter of personal preference, I’m clamping the values between 0 and 999 but I appreciate I should also place a clamp/limit on the maximum number of gems that can be collected by the player (rather than just the display as I’m doing here). I also prefer not to show “0” gems to the player unless its part of the shop’s interface, so I’m checking to see whether the player has any gems at all (otherwise I just assign an empty string to the TMPro object’s text field.

The final part of the puzzle now is in updating the player’s inventory (which requires us to first make some sort of inventory 😅). Since this is a fairly short and simple game, I’m not going to go for anything more advanced than a Dictionary to store an Item struct and a value for the quantity of that item collected. I’ll give that it’s own article, and that’ll conclude our shop system setup 😉

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store