Setting up Shop! Creating a shop in a mobile adventure game (1): Purchase Options

Marcus Ansley
3 min readOct 6, 2021

--

To go with our loot/diamonds collection system, I’ve recently implemented this little shop to the mobile 2D adventure game I’ve been working on for the course 😉 Essentially, we’re checking for OnTriggerEnter2D, setting a canvas to active, then letting the player interact with the canvas by clicking on the item they wish to purchase followed by the “Buy Item” button. This’ll likely be another multi-article topic, but let’s get stuck in.

Setting Up the Purchase Options

On each of the buttons, I’ve added an OnClick event to call a public method in an appropriate script. In the case of the Buy Item button, I’m calling ShopKeeper.AttemptPurchase (as we need to check whether the player’s got sufficient funds before actually purchasing the item):

For the purchase options, I tried to make this as modular as I could so that we can reuse the shop but change the items getting sold there. So as it is, those buttons call a public SelectItem method on their respective PurchaseOption script. There are a few things going on with these PurchaseOption scripts, so I’ll show what it looks like in the Inspector then try to give a bit of a walkthrough:

As for the script, it’ll either use a specified ShopKeeper or it’ll try to find one, then it’ll set the name and price of the item according to a struct of type “Item”. Every time an option is selected by a user, I’m sending that item’s information to the ShopKeeper via its own public SelectItem method (which accepts an Item and an integer id):

As you might’ve noticed, I’ve also got an option to check whether this is the default item (i.e., the item selected by default if the player doesn’t choose anything). This just gets us around a bug where the player can buy a non-existent item (since no item information has been passed onto the ShopKeeper). To be honest, I’m not entirely happy with this solution and think I might just have the Buy Item button get set to gray and inactive until an item has been chosen (but for the time being this works just fine 😛).

Item Struct (and ItemType enum)

As for what exactly an Item is, I’ve defined this as well as an ItemType enum in a static script called VariableContainer:

I was originally planning to write down a list of integer IDs for the various items in the game, but have instead opted for this ItemType enum (which will store any items which can be bought, looted, or otherwise obtained by the player).We can continually add to this ItemType enum as the game grows, and as long as we assign unique integer values to each of the elements there shouldn’t be much risk of us messing up any things in the Inspector.

The other two fields, “value” and “name” just store the price/value of the item and its display name respectively (as we want it to appear to the player). With this, we’ve packaged up the key information we need to know for each of our items in the game! 😄 As for what happens when the ShopKeeper receives this information, I’ll be covering that shortly in the next article 😉

--

--