**This is an old revision of the document!**

Add coins and display the number of collected coins

This tutorial is part of the How to make a platform game tutorial, but you can also re-use it for any other game where you can collect items!

In the platformer game we've created so far, the player has nothing much to do: let's add coins for the player to collect.

Add coins

Start by creating a new Sprite object with an animation with the coinGold image:

You can name this object “Coin”. You can then put several coins in the level. As you'll need several coins, it's a good idea to use the “drag and clone” feature of the scene editor: select a coin, keep pressing Ctrl (or Cmd on macOS), and drag the coin. This will duplicate it.

If you hold Shift while moving, you'll also be able to move the coin created on the same axis that the original one.

Delete coins when collected

Now, let's destroy the coin object when the player touch them. Add a new event and add a condition. Choose the condition to test for a collision between two objects. The first object should be Player and the second object should be Coin:

You can then add an action that will delete the coin. Only Coin objects that are picked by the first condition will be used by the action, so only the coin that was colliding with the player will be deleted.

Add an action “Delete an object” (from category Common actions for all objects > Objects) and enter Coin as the object to be deleted:

The event should look like this:

You can now launch a preview and see that coin are deleted when the player run on them!

Play a sound when collecting a coin

To make the game feel more dynamic and rewarding, let's play a sound when a coin is collected. It's easy: we already have a sound file called sound.wav in our game folder (if that's not the case, you can download resources here and extract them in your game folder).

It is something we haven't done before: we can add multiple actions to execute to a single event. To play a sound, let's add another action to the event that we used to delete the coins. Just click 'add an action' right below the 'Delete object Coin' action and add the action called Play a sound, from the Audio category so we are not only deleting the coin but also playing the sound.

There are a bunch of parameters available for playing a sound. The first and most important is the sound file to be played. Click on the field and then on “Choose a new audio file”.

Choose the file coin.wav that you downloaded with the rest of the assets and that you put in the game folder:

Leave the other parameters as they are (i.e: don't repeat the sound, volume at 100, and the default speed for the sound).

That's all! Just launch the preview and the sound will play anytime you collect a coin 👍

Increase a score using variables

We're going to use a variable to update and remember the score of the player. Variable are containers that we can use to store and remember certain information such as numbers and strings. In GDevelop there are different type of variables:

  • Object variables are private to each instance of an object meaning each instance can store it very own values without being shared among other instances. It is useful for example when we want to effect the life of each enemy for example.
    • Scene variables (the most used ones) are private to each scene. This means that their value is not shared among other scenes in our game. It is useful to store information only relevant to the currently played scene. For example, the life of the player or a score.
    • Global variables, as the name suggests, are global in the whole game. Values are shared among all scenes in the entire game. It is useful if we would like to store information that we want to share between all scenes such as the the amount of bullet or amount of money the player has.

    The variable we're going to use is a Scene variable and will be called “Score”.

Let's add an action in the latest event we've made. Choose action Variables > Value of a variable. This action will be used to add 100 to the Score variable, so fill the parameter as shown here:

In GDevelop we don't need to declare variables before using them, we can use any variable without declaring it by just naming it in an action or a condition. In case we are using an action on a variable that is not declared, GDevelop will create it automatically for us with the default value of '0' or an empty string “” and after execute the action on the variable to save us from any trouble.

But, to better organise our game, it's always a good practice to declare variables before using them. This way it's easier to remember what variables we are using and we can also change their default value if we want it to be something other than 0. To declare a variable, just click on the button “...” on the right of the variable name to show the list of variables:

The list is shown and is empty as no variable was declared yet:

You can click on the + button to add a variable, and enter “Score” as its name:

Remember that this is entirely optional, only a good practice.

Display a score using a text object

Add text object

For now, the variable Score can't be seen so we can't verify that the score is increased.

We need to use a Text object to display the value of the Score variable. Let's add a new object. This time choose Text when asked for which type of object to create:

Change then the object properties so that the text is black and bigger than the default font size:

You can then place the object on the scene:

Changing text layer

If you launch the game, you can see that the Score text is displayed with the rest of the objects and it position is not fixed on the screen.
We can easily solve this problem by using a layer on top of the base layer. Open the layer editor using this button:

The list of layers is shown, with only the base layer for now:

Click on the + button to add a layer. Enter “UI” for its name (UI stand for User Interface).

Finally, move the text object to this new layer by selecting the text object and in the properties on the left, select the UI layer:

Make sure that the object is displayed somewhere visible on the screen, for example put it on the top-left. The black rectangle on the scene editor represents the size of the window:

Display the score value

Now that we have a text on our screen, it time to make it display the value of the Score variable. Let add a new event. This event won't have any condition meaning the action is going to be executed every single frame, about 60 times a second.

Add an action and choose the one in Text Object category called Modify the text.

For the first parameter, choose the Score object, as it is this object that we want to update. For the second parameter, choose = (set to) operator (we want to set the text to a new value). Finally, the last parameter represents the new content of the text. It's a string expression that will be evalutated during the game by GDevelop when the action is run.

We can specify a text by writing it between quotes (like this: "Hello World"). But we can also use something called expressions which is a function that is return a certain value which we can use in some way or form. In this case, we need the value of the Score variable and we want to use that value to update the text object. Expressions is somewhat advanced topic so I don't think it would be the right time to get deep in to it.

What you need to know for now is that, in order to get the value of a scene variable we need to use the expression Variable(variableName). And because it is a number variable but we need the value as text, we need to convert the value returned from this expression to a string and we can do that by using an other expressions called ToString(valueToConvert). So the actual expression need to look like this ToString(Variable(Score)).

Finally, we can add this expression after a static text using the + operator: "Score : " + ToString(Variable(Score)).

Lots of other functions, expressions are available in GDevelop. When combined, you create powerful actions that do advanced work. You can find more expressions in the expression editor by clicking on the blue icon next to the value fields:

Here is the final event:

You can preview the game and see that the score is shown and updated when you collect a coin!

Bonus: fixing the score bug

You might have noticed that if you collect two coins at the same time, the score only counts once. For example, if each coin is worth 100 points and you collect 2 coins, the score will only go up 100 instead of 200. The fix for this is easy and is something we haven't looked at yet: an event called For Each Object. What we will do is tell the engine that for every single coin, add 100 to the score.

To start, Click the giant + symbol at the top,

Select for each object,

and it will add an event at the bottom of the event page. Drag it up and make it a sub-event of the player collision with the coin:

Click to choose an object. Type coin in the field that appears, and click on the object.

Now that you've added the object to the sub event, lets move the actions down to the sub event. Hover over the first action, right-click and cut it, then hover over the action in the sub event, right-click, and paste. do this for each of other two actions.

This is what the final event should look like:

Now run the game, and you will see if you gather two coins at the same time, the score will update to reflect that you indeed collected two rather than one.

Next step : add enemies

Every platform game comes with enemies that you have to avoid or kill by jumping on them! Let's add some and see how we can make them move.

➡️ Read Add enemies to your platformer game!