Day 125 — Purchasing an Item From the Shop

Hey and welcome!

In this one we’re going to wrap things up with the shop system and I’ll show you how you can purchase an item from it and let the game manager know about the purchase.

Let’s start thing off by editing the SelectItem() function in the Shop script:

public int selectedItem;
public int itemCost;
public void SelectItem(int item)
{
//0 = flame sword
//1 = flight boots
//2 = castle key
switch(item)
{
case 0:
UIManager.Instance.UpdateShopSelection(58);
selectedItem = 0;
itemCost = 200;
break;
case 1:
UIManager.Instance.UpdateShopSelection(-54);
selectedItem = 1;
itemCost = 400;

break;
case 2:
UIManager.Instance.UpdateShopSelection(-162);
selectedItem = 2;
itemCost = 100;
break;
default:
Debug.Log("Please assign an ID between 0-2");
break;
}
}

Here we’re creating two variables, one to store a reference to the current selected item depending on which case has been called and then another to let us know how much that item is.

In order to make use of those variables we’ll add in this new function:

private Player _player;public void BuyItem()
{
if (_player.Diamonds >= itemCost)
{
_player.Diamonds -= itemCost;
Debug.Log("You've purchased: " + selectedItem);
Debug.Log("Gems remaining: " + _player.Diamonds);
_shopUI.SetActive(false);
}
else
{
Debug.Log("You have not got enough gems");
_shopUI.SetActive(false);
}
}

This function we’ll call using the OnClick() event again similar to the SelectItem() function. Here we’re checking if the player has enough gems on them to purchase the current selected item, if they do then we subtract the cost of the item from the player’s gem count, log a couple of messages to the console and then close the shop window.

If the player doesn’t have enough then we send a log to the console letting us know that there aren’t enough gems and we close down the shop window again.

Also when the shop detects the collision with the player let’s change the local reference to the player into a global one instead by assigning the component to the new _player variable that we’ve created.

Once you’ve done the code go ahead and add in the event to your OnClick() for the buy item button and you should have something like this working for you now:

Next we need a Game Manager that keeps track of which item or items we have in our possession but for the time being we’ll set it to just keep track of the castle key which is needed to complete the level.

Create and empty object and call it GameManager and then create a GameManager script with this code in it:

private static GameManager _instance;
public static GameManager Instance
{
get
{
if (_instance == null)
{
Debug.LogError("GameManager is null");
}
return _instance;
}
}
public bool HasKey { get; set; }private void Awake()
{
_instance = this;
}

This has been set up again using the singleton design so that we can directly reference the GameManager instead of needing to store a reference to it in the shop script when the game starts. The important bit of code here though is our HasKey property that tracks whether or not the player has purchased the key.

Lastly go ahead and add this bit of code to your BuyItem() function in the Shop script:

public void BuyItem()
{
if (_player.Diamonds >= itemCost)
{
if (selectedItem == 2)
{
GameManager.Instance.HasKey = true;
}
_player.Diamonds -= itemCost;
Debug.Log("You've purchased: " + selectedItem);
Debug.Log("Gems remaining: " + _player.Diamonds);
_shopUI.SetActive(false);
}
else
{
Debug.Log("You have not got enough gems");
_shopUI.SetActive(false);
}
}

This sets that HasKey property to true if the current selected item is 2 which is the number we’ve given our castle key.

With all that done you now have a way to purchase a key for 100 gems and complete the level!

Hey and welcome! My name is Connor and my goal here is to put out a daily post for a full year about my game development journey.