In the previous lesson we learned
to add background objects and then we design and develop the platform furthermore.
In this tutorial, we are going to learn how to add collectible items and a
score system. Here we are going to add some coins as the collectible items and
we are going to give points when the player collects them.
We are going to add a Sprite Object.
Go to assets folder > coin and add all the images to the
animation. Enable loop option to keep the animation playing continuously. Name the object as “Coin”.
Now drag and drop the coin on to
the canvas. Adjust the size that fits to the scene. Now as we did in the previous
tutorial, you can clone the object by press and holding Ctrl (Cmd on Mac) while
dragging. And to keep the coins align, press and hold shift while you are moving
the cloned coin object. This will help you to move the cloned coin in the same
axis of the original coin, thus aligning the coins properly. Place few coins in
random places in your scene.
Now here we come to the logic
part. When the player hits a coin, that coin should disappear and at the same
time the player gets points. Let’s see how it is done through events. First, we’ll
delete the coin whenever the player hits it. Go to Event editor and add a new
empty event. Now go to add condition and search and select “Collision”. (Note:
there are few types of collision conditions. What we use here is “Common
conditions for all Objects/Collision” condition). Select first object as “Bob”
and the second object as “Coin” and click OK.
Now go to actions and search and
select “Delete an object”. Select “Coin” as the object and click OK. Now
let’s play a sound when the coin disappears. We need to add another action
right below the delete action. Go to action and search and select “Play a
sound”. Click Choose anew audio file from the Choose the audio
file to use drop down menu. Navigate to our assets folder > audio
> coin.wav and add the audio file. Make sure Repeat the sound
option is set to “No”. Keep other settings as it is and click OK.
Hit the preview button to preview the changes we did so far. Next, we need to give points when a coin is collected, and we need to display the total points the player scored. We need to store the score to display it. For that we need variables. Variables are used to store information. It’s like a potion of memory for computers. If you are familiar with programming you already know what a variable is. If you have no experience in programming, don’t worry, that doesn’t matter much.
There are three types of variable
we use in GDevelop. According to official GDevelop documentation;
- Object variables are private variables, specific to each instance of an object. This means that each instance can store its very own values, without them being shared with the other instances. It can, for example, be useful if we want to change the life of each enemy.
- Scene variables (the most used ones) are variables attached to a scene. This means that their values are not shared with 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 with 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 amount of bullet or amount of money the player has.
What we are going to use here is a
Scene Variable. Normally you don’t need to create a variable at the beginning.
You can create one and use it on the spot. Go to action in the same event we
created for deleting coin. Search and select “Value of a scene variable”.
Click the button next to Variable drop down (drop down must be empty as
we have not defined any variable yet). This will open the “Scene Variables”
popup window. Currently it is empty as we have not created any variable yet.
Click the + button to add a new variable. Set the variable name as “Score”.
Set the default value to “0” and click APPLY. Now you will be able to
select “Score” variable from the Variable drop down menu. We are going
to give 10 points for each coin the player collects. So, set the Modification’s
sign to + (add) and set Value to 10. Click OK to save
the action.
Let’s display the score in the
scene. We need to use a Text Object to display the score. First, we need
to add a new layer to hold the Text Object. Why? If we add the Text Object to
the default layer, when the player runs, the Text Object will not remain fixed
to the screen position. It will act as other background objects in the scene.
To avoid that, we are going to place the Text Object in a different layer. So,
open the Layers editor (as we did to change the color in the previous
lesson) and click ADD A LAYER. Rename the new layer to “UI” and close
the Layers editor.
Add a Text Object (from Objects
panel). Name it as “ScoreDisplay”. Set font size to 36, enable Bold and change
the text to “Score :” (you can change the color if you like) and click APPLY.
Now switch to Event editor. Add a
new action to the coin deleting event. Search and select “Modify the text”.
Select the object “ScoreDisplay” as the object. Set the Modification’s sign
as = (set to). Here we can’t assign the Score variable as value. Since
this is a text object, it only displays text not numbers. Well, even numbers
are displayed as text. Our Score variable is a number. We need to
convert it in to text. If you are familiar with programming, you can understand
what is going on here. Well I’m going to show this process in a short video for
you to understand.
Well, if you did everything
correct, your event should look something like this;
Hit the preview button and preview
the game. And voila! The score shows for each coin the player collects. In the
next lesson we are going to learn another important thing.
If you have any doubts in this
lesson. Please feel free to ask it in the comment section below.