**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” (in 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).

Add an action after the action that deletes the coin. Choose action Play a sound, in Audio category. There are a bunch of parameters available. Enter the name of the sound file for the first parameter: coin.wav. Let 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 useful to remember all kind of information. There are scene variables (the most used ones), global variables (variables that are shared between all scenes of the game) and objects variables (which are specific to each instance of an object. There are also very useful and we'll use one for creating an enemy later).

The variable we're going to use will be called “Score”.

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:

As you can see, we can use any variable without declaring it by just naming it in an action or a condition. GDevelop will create it in memory with a default value of 0.

To better organise your game, it's a good idea to declare your variables: it's easier to see what variables you are using and you can change their default value. If you want to do this, just click on the button “...” on the right of the variable name to show the list of declared 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're going to display the score using a Text object. On the scene editor, add an object, and choose Text when asked for which kind 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 put 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 is not fixed on the screen.
It's easy to fix this by using 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 on this new layer. To do this, select it 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

We now want to update the text object to display the value of the Score variable. To do this, add a new event. This event won't have any condition (so the actions will run at each new image rendered, about 60 times a second) and a single action.

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"). The thing truly interesting is that we can then concatenate this text with other values, coming from the scene variables, but also objects properties, like an object position, the variables stored inside an object…

These values are provided by functions, that take parameters (much like actions and conditions !). For example, the function to read the value of a variable is called Variable and take the name of the variable as parameter: Variable(Score).
The Variable function returns a number. If we want to use it here for the text, we have to convert the number to a string using ToString function. We can compose the two functions: ToString(Variable(Scene)).

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

Lots of other functions are available. When combined, you create powerful actions that do advanced work.

Here is the final event:

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

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!