Godot. Create a recollectable item

Submitted by tech4life on

To make our games more playable, it is sometimes interesting to add items or objects that the player can pick up to increase their skills in the game.

In this mini tutorial we are going to give you the keys to create an item that our player, in our case a ship, can pick up and increase one of its skills. In this case it will be a shield and we will teach you how to pick up the item and activate the shield.

The first step is to create the scene that corresponds to our item.

In our case we have created a scene with a node, two 2D sprites and a collision shape. One of the sprites is a green circle, and the second sprite is the shield, which will go inside. Finally, the Collision Shape is of the CircleShape2D type, covering the entire sprite.

In our game, items and enemies fall from the top of the screen at a certain speed, and the player can only move left and right to pick up items or avoid enemies. For this reason we have created a script linked to our shield:

In it we set a random initial speed, based on a range and the current level. And then simply in the _physics_process function we indicate that the position and will vary (fall) and that when it reaches a certain point (700) it will disappear (queue_free), that is, once it leaves the screen.

Finally, in this part, we have given our node a group name, in the node tab, Groups subtab. This is important for later, to identify when the player collides with the object to know what type it is.

The next step is to go to our player, and in the function : _on_area_entered add the following code.

In this case we indicate that if the area that has collided with the player is from the shield group (which is the group we have given to the shield) do the following code. In our case, we tell it that the shield disappears from the screen (queue_free) and then we set a global variable indicating that we have taken the shield item and finally we emit a signal that in our case we have declared in a Global class.

The next step is to go to the script of our main or game scene and in the _ready function make the following assignment:

This is to tell you that when the activateShield signal is fired, we will pick it up here and call the activateShield function in our main scene.

In this function, all we will do is activate a timer that we have added to the scene so that the shield only lasts a few seconds, and we will capture the end signal of the timer to indicate again that the shield is no longer active.

In our player scene, we have also added the shield, with a new sprite

And in the player code we have added the following code:

Here we simply look at the Global variable that we have to know if the shield should be shown or not.

Finally we need to see how the shield acts allowing enemies to not harm the player. To do this, again in the player script we have added this:

In the _on_area_entered function, when a meteor-type object collides, we check to see if the shield is active. If yes, we simply remove the meteor/enemy. If not, we indicate that the player is dead.

This is an easy and simple way to create an item that the player can pick up and a fairly basic functionality that can be improved but for a simple game it works perfectly.

Nivel