How to Create Pickups in GameMaker


How to Create Pickups in GameMaker

In this tutorial, you'll create custom items that a player can pick up in your games, like health, power-ups, or collectables.

Create a new project using the Windy Woods template. You can choose either GML Code or GML Visual:

undefined

You can use your existing Windy Woods project if you already have one.

Importing a Local Package

This tutorial will use two new sprites, spr_pickup_health and spr_pickup_pot. You can either create your own sprites, or download our asset package.

To import this package, click on Tools and select “Import Local Package”.

undefined

Open the asset package you just downloaded.

Choose “Add All”, and click “Import”.

undefined

You now have all the assets you need for this tutorial!

Create a Heart Pickup

Create an object and name it obj_pickup_health.

undefined

I've put this new object under Objects -> Environment -> Items.

Assign it the sprite spr_pickup_health (found under Sprites -> Items).

undefined

It might sound more natural to name this object obj_health_pickup; however, by putting "pickup" first, it becomes easier to tell what this object is, and that becomes more and more important as you add more pickup objects.

Colliding with Pickup

Let's make the player able to collect this pickup and gain a heart.

In your new object, add a Collision event with obj_player.

undefined

Add the following code.

if (other.hp < other.max_hp)
{
        other.hp += 1;

        instance_destroy();
}

undefined

This code references the instance variables hp and max_hp created in obj_character_parent.

It checks if the player's hp is less than its max_hp, and if it is, it increases the player's HP by one.

hp and max_hp are instance variables. Instance variables are unique to an instance of an object. For example, each enemy's hp can be different.

When one instance wants to change another instance's variable, it has to "get" it in some way. In the Collision event, you can do so by writing other before a variable’s name.

other lets you read or change a variable in the other instance that's colliding with you. In our event, the pickup is colliding with the player, so we're reading and modifying the hp variable in that player instance.

Read more about the keyword other in the manual.

Placing the Pickup

Go to rm_level_1:

undefined

Select the "Instances" layer:

undefined

Add an instance of obj_enemy1 (found under Objects -> Characters -> Enemies) and a few health pickups.

undefined

If you collide with a health pickup at full HP, nothing happens. That's because you can only collect a heart pickup if the player's health is less than max.

However, if your player gets hurt, you can collect a heart pickup to recover health:

How to Display a Particle Effect in GameMaker

Let's give our pickup some much needed oomph, by showing a particle effect.

Open obj_pickup_health's collision event with obj_player.

Add the following code inside the main If condition, at the end:

effect_create_above(ef_ring, x, y, 1, c_fuchsia);

undefined

Now when you collect a pickup, you'll see a ring effect. You can play around with the parameters of this function, and create the effect you like!

How to Create a Custom Pickup in GameMaker

We'll create a new "pot" pickup, which the player can collect.

This means we need a variable to store how many pots the player has collected.

Create a new variable in the obj_player’s Create event:

pots_collected = 0;

undefined

You'll find obj_player under Objects -> Characters -> Player.

You can search for any asset using the keyboard shortcut CTRL/CMD T.

Let's create the pickup now.

Create a new object and name it obj_pickup_pot. Give it the sprite spr_pickup_pot (Sprites -> Items).

I put this object under Objects -> Environment -> Items

In your new object, add a Collision event with obj_player.

undefined

Add the following code:

other.pots_collected += 1;

instance_destroy();

effect_create_above(5, x, y, 2, c_green);

undefined

This code increases the player's pots_collected variable by one, and then destroys the pot itself.

It then creates a smoke effect.

Because we're in a collision event with obj_player, we're using other. to reference its variables.

Temporary HUD

We want to be able to see how many pots the player has collected, so let's add some temporary code to draw that value.

Open obj_player, and add the Draw End event:

undefined

Add the following code in the player’s Draw End event:

draw_text(x, y - 150, "Pots collected: " + string(pots_collected));


undefined

This will draw the number of pots collected next to the player.

Now, open rm_level_1 and place some pots! Run the game and collect all of them.

For the moment we're drawing the number of pots collected next to the player. This works, but it could be better.

Create custom HUD for the pots to add some polish. Animate these pickups using Sequences.

Summary

Some key takeaways from this tutorial:

Read more tutorials to build your game.

Happy GameMaking!