Adding a Virtual Shop To Your Game Part 1

If your game’s players have beaten or are starting to get bored of your game, building a shop for unlock-able 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 the player to get more points! If you want to get more points per click, upgrade your “Player”.

To start, we are going to setup our money that will be shared across all the scenes.


Adding Money

The first thing we’re going to do is to go on the Global UI and add a Label that will represent our money and set the text to be 0. Also, add another label to tell us what that label is.

Lets also, move the money label slightly off screen so it will never show up when you play your game.

Now we’re going to handle saving and loading the money, so that every time the player leaves your game, the money they’ve earned will still be there!


Saving Money

Go into the money label’s behaviors.

We’re going to handle updating our money first.

We’re going to need a Behavior Bundle, a Get Label, a Save Value, and a Broadcast Message behavior. 

Arrange the behaviors like in the picture above and set the Behavior Bundle to OFF.

A lot went on there. Lets go through what it all means.

When something executes the Behavior Bundle, we want to Get Label to grab the current value of money. If you’re wondering why we didn’t use a Box Container for this, its because there isn’t a way to get the value of a Box Container while in another object… YET.

Next, we want to save that Get Label value to file. To do that, simply drag the output from Get Label, and drop it into the Value To Save input field for Save Value.

The Broadcast Message behavior lets other object know that the money has just been updated. You can use this to update labels in your scene with the new money value.


Loading Money

Now that we have saving out of the way, lets make it so that when the player opens up your app, it loads the money that the player earned!

Inside of the Money label, we need to add a Behavior Bundle, Load Value, another Behavior Bundle, and a Behavior On. Arrange those like in the picture below.

Now add an If behavior, another If and drag it onto the side of the first If to make an Else If, a Change Label, and a Behavior On. Make the Behavior On turn on the Broadcast message from earlier.

Arrange those behaviors like the picture below:



Okay… A lot is going on here so lets take a look at what we just built!

You’re probably wondering what the purpose for Behavior Bundles are. Behavior Bundles serve two purposes… Firstly, it organizes your behaviors, and secondly, it forces the order of behaviors so that they execute from left to right like an event does.

So we connect our Load Value behavior to the Behavior Bundle so that when the scene starts, it loads the value stored on file. In the Load Value properties, make sure you choose the Save Value behavior that we created before.

The next Behavior Bundle is going to check if a value was loaded or not. The If Behavior is going to change the label if the value is not empty. To do this, set the first input field to be the output from the Load Value Behavior and the second input field blank.

The If Behavior then connects to the Change Label and only gets executed if the Load Value actually loaded a value.

The Else Behavior doesn’t need to check anything. It will always just be true and execute its child Behavior On. The Behavior On will simply execute the Save Behavior Bundle we created earlier so that we can save a default amount of money the player has.


Spending money!

Now that we have loading and saving our money out of the way, lets make a way to spend it! To do that we are going to build out a consolidated transaction system out of Labels.

To start, on the Main Canvas, drag on three labels. The first is going to represent the current transaction amount, the second is going to represent the status, and the third is going to represent the message like “Not enough Money!” or “Purchased!”. Also, lets label our representations so that we know what they are. Lets also move everything slightly off screen so that they aren’t visible to our player when playing the game.

Lets also drag on another object onto our canvas to handle the transaction logic. In this example, we’ll just use a label that says “Transaction Logic”.

Go into the behaviors for the Transaction Logic label so we can create the logic for handling the logic of a purchase.

What we need to do is check if the player has enough money to complete the transaction. To do this, drag in a Behavior Bundle, and two Get Label behaviors and connect them like the picture below. Make sure to set the Behavior Bundle to off so we don’t prematurely cause a transaction when the scene loads.

The first Get Label, is getting the money label, and the second one is getting the Transaction amount. What we want to check is to see if the money is less than the transaction amount. If it is, we want to change the transaction status to “Failed” and the message to say “Not enough money!”.

To do that, add an If Behavior and two change labels. Arrange the behaviors like the picture below.

The If behavior just checks to see if the money label is less than the transaction amount.


Now we’re going to check the reverse. If the user does have enough money lets subtract the transaction amount from the money and save the money. To do that, create an Else If behavior like we did before, connect a Behavior Bundle to organize the rest of our logic, a Subtract Values behavior, a Change Label Behavior, and a Behavior On.

The Subtract Values behavior is going to subtract the Transaction amount from the money.

Then we use the Change Label to change the money label to be the output of the Subtract Values behavior.

The Behavior On behavior will then execute the Save behavior bundle that we created in the Saving Money section.

Now lets create a success status so that the action that executes the transaction knows whether or not the transaction was successful!

To do that, simply drag on two Change Label behaviors.

Set the status label to say “Success”, and the transaction message to say “Purchased!”

So now we have a way of spending money. By setting the transaction amount with a Change Label behavior, all we have to do use a Behavior On on the Behavior Bundle that triggers our transaction logic. To check if it was successful or not, all you have to check is the Status label to see if it says, “Fail” or “Success”.


Earning Money!

We’ve got our players spending money… but how can they spend what they don’t have? We need to give our players a method of earning some money to spend. Still in our purchase logic, add another Behavior Bundle to handle our purchase logic and make sure to set it to off. We don’t want to prematurely buy something when the scene starts.

The first thing we are going to do is get the Money and Transaction amount. So add in two Get Label behaviors like below.

Then we want to Add the Money to the Transaction amount. Add in an Add Value Behavior, Change Label Behavior and a Behavior On.

The Change Label behavior is going to be the output of the Add Value behavior and it changing the Money value. The Behavior On is going to Save our new money value like we did in the last section.

Finally, We want to create a transaction status and message. Add in two Change label behaviors to set the Status to “Success” and the message to say something like “Sold!”.


And now we have the ability for our players to earn money. Simply call change the Transaction amount and use Behavior On to execute our earning Behavior Bundle so that it adds to the user’s money.


Where To Go From Here

So far we’ve made a transaction system where our players have money and can use that money to make purchases. Our transaction system also gives the ability to let our player earn money as well. The transaction system also persists the money so that the player can leave the app and come back and still have the same amount of money.

In the next tutorial, we are going to cover buying items for our players and making sure that their items stay persisted as well.





Article is closed for comments.