If your game’s players have beaten or are starting to get bored of your game, building a shop for unlockable items or characters is a great way to build replay value and retention. In this tutorial, we are going to cover how to build an in app shop where your users “Buy” items with the points they generated in your game!
In this example, our game will be very simple: just keep clicking to get more points! If you want to get more points per click, upgrade your “Player”. Feel free to make your game as simple or complex as you want. To start, we are going to setup our currency system that will be shared across all scenes.
The first thing we’re going to do is to go on the Global UI and add a Label that will represent our currency and set the text to be 0. Also, add another label to tell us what that label is. The player will be able to see how much money they have. Feel free to stylize the labels to your liking!
We want to make sure that the amount of money we have saves between sessions. This means having the money amount save during gameplay, and also having the amount be loaded when the player is returning to the game. We can select the label representing our currency amount and edit its behaviors to make this work.
Let's start by working on saving the currency amount to file. We want to be able to trigger saving from anywhere in the scene, so let's add a Receive Message behavior. In our case, let's have it listen to the saveMoney event.
This behavior can be triggered by any Broadcast Message behavior in the scene broadcasting with the saveMoney event. This means we'll be able to trigger saving whenever and wherever we want.
Now, we want to get the currency amount to save, so let's add Get Label and connect Receive Message to it. Make sure you're getting the value of the currency amount label.
Now that we have the currency amount we want to save, let's add a Modify Save File behavior and connect Get Label to it. In the behavior, make sure the modification is set to Set Key and the key type is set to Existing.
Tap on Select or Create Key to create a key and enter Money in the text field. Press the + button to create the key. Your behavior should now be referring to the Money key. This key will be tied to the currency amount that will be stored to the save file, so when we want to retrieve it later, we can refer to the same key again. You can name your key anything you want, just make sure you use the same key to retrieve the same saved data. Your behavior should look like what is shown below.
Now, we want to get the text output from the Get Label behavior and plug it into the Modify Save File behavior.
Congratulations, the saving functionality complete! What the behaviors above are doing is getting the label (representing the currency amount) and then saving it to file every time the saveMoney event is broadcasted from a Broadcast Message behavior.
We'll get to how to trigger saving later, but for now, let's work on loading the currency amount from file. Add a Frame Event behavior and set it to trigger on Scene Start. This is what will trigger the loading before the scene runs. Make sure Trigger on Restart is enabled so this can trigger when the scene also restarts.
Add a Load from File behavior, then connect the Frame Event behavior to it. From here, you can select the preexisting Money key that we've created earlier.
The Default Value is the amount of currency the player should start with. In our case, we'll enter 1000. The default value is what will be loaded if the player does not have a previously saved value associated with a key.
At this point, we have successfully loaded the value but we're not doing anything with it. In this case, we want to update the currency amount label to display the loaded value. Let's add a Set Label behavior to do just that!
Plug the loaded value from Load from File to Set Label like so.
Congratulations, you can now save and load currency! Your behaviors should look something like shown below - on the left is the saving functionality and on the right is the loading functionality.
We haven't actually added a way to earn currency yet. You can have your player earn it through various means, but in our case, we'll keep it simple and have the player make money by tapping a cookie. Sound familiar?
We have a cookie graphic in the scene and we want to make it so when we tap on it, we earn one point. Let's edit its behaviors.
Now let's have it so when the player touches the cookie, it'll add the money amount by 1. Make sure the correct label is selected!
This works but it doesn't save yet. After adding or subtracting currency, we should save changes by using the Broadcast Message behavior like so - this behavior should be used every time to trigger a save to occur. We're reusing the saveMoney event key that we have used in the Receive Message behavior when we were creating the save functionality.
Awesome, now you've got a working persistent currency system! At this point, you can tap the cookie as much as you want and it'll save your progress. When you leave and come back, your currency amount should be where it previously was.
Now that you've got a proper currency system going on, let's add a way for your player to spend their money and get something in return! Create a new layer called Shop. This is where we can design our shop the way we want. Feel free to design it however you like.
We're going to have only two items in the shop for the sake of time and ease, but you can add as many as you want. In this case, our items are:
Make sure there's a way for the player to actually enter and exit the shop. In this case, we're simply hiding and showing the Shop layer. There are no other behaviors other than these.
Just like what we did with the currency amount label, let's make the Click Multiplier and Passive Income load and save the same way. We'll use the exact same behaviors like before.
Now, let's work on the Click Multiplier item. We will go to the BUY label under it and edit its behaviors.
Have it so when the player touches the BUY label, we first get the amount of currency the player has. We can use Get Label and have it select the currency amount label.
You can have an If behavior check if the player doesn't have enough money to buy this item. You can alert the player if so. In this case, if the player has less than 100 points, then the Alert behavior will notify the player.
In the other hand, if the player does have enough currency to buy this item, then we can simply add Click Multiplier by 1.
Don't forget to also subtract the currency by the cost and save it! You wouldn't want your players to get this item for free, right?
Let's do the same thing for Passive Income. The only that changes is the cost, which label to add to and what to save.
Great job, now you have a functioning shop! But these items currently don't have any effect in gameplay and are solely just numbers - let's work on that next.
We have already established what we want our shop items to do:
Let's make these items do what they should actually do - starting with Click Multiplier. In the behaviors of the cookie, we can have the multiplier value loaded from file like so.
Then we can use the loaded value as the amount to add to the currency.
Every time we tap on the cookie, the multiplier is retrieved from file then it is added to the currency. With that done, we can now work on the Passive Income. Let's add a label and name it accordingly - this will be where the behaviors for this will be. This label shouldn't be visible to the player, so hide it outside of the screen.
In the behaviors, we can set up a 1 second Timer which repeatedly gets the Passive Income amount.
Now, we want to have the currency increase by the passive income amount and have it save each time like so.
Well done! You now have a working shop and currency system! Although it doesn't look very pretty, you can stylize it to your like and expand on it.
You are able to buy useful items in the shop with your money. Feel free to add more shop items, more ways to earn currency or even polish it! Currently, the prices of the items don't change when buying them. This makes it super easy to buy them as you earn more. Do you think you can make the prices increase as you buy them?
Feel free to add different shops and use the currency system in other scenes!
If you would like to download the project for yourself, download it here!
Be the first to hear about new blogs, updates and fun events!