Adding Lives to your Game

 

Before you start, you will need either a Maze or Platform engine character.
You will also need at least two direction sprites, left and right.

Download the Basic Movement engine here
Download the the Basic Platform engine here

You will also need an enemy or something that will take away a life.

 

When using something like lives, health or score in a game, you have to use track of them using variables.

 

Create a new object and call it, o_setgame

This object will set how many lives you start with in the game, the same object is later used to set other varibales such as score, health, coins ect
In game maker, there are actions to set, check and draw the lives, health and score variables. These variables are global variables that are already exist in the game, it's up to you to set and use them.

Here are the actions used to set, check and draw the lives on the screen.

Action Lives Set Action Lives Check Action Lives Draw Image Action Lives Draw Image

 

Setting the lives at the start of the game

Add a Create event to the o_setgame object, on this event, you will set your lives and health ect. Event Create

From the Score tab, drag in the Set Lives Action Action Lives Set

Set the New Lives as, 3

You MUST put this o_setgame object in your first room, if you don't, the lives won't be set when the game starts. You can also put this object in your game over screen to reset the lives and score for when the player continues the game. Every time you go to a room with this object in, it will reset the lives and anything else you set on it so be careful where you use it.

Doing this will set the Lives as 3 when the game is started.

 

Losing a life when you hit an enemy

Now we want to set the game up so when you hit an enemy, you lose a life.
This is very simple, it's just a collision event, set lives and jump to start position action.

Open your Character Object

Add a Collision Event with the enemy Character or if you're using parenting, the parent enemy character. Event Collision

Add the Set Lives Action Action Lives Set
Set the New Lives as, -1 and tick relative, this will take away one life from your total.

From the Main panel, drag in the Jump to Start Position Action Action Jump to Start Position
This action will make your character go back to the start of the level.

Your collision event now takes away one life and puts your character back at the start position. The next thing we have to do is end the game when there are no more lives, if you don't, you can keep losing lives and nothing will happen when you go below zero.

You can also make your enemy losing a life when it falls down a hole, to do this, put the same actions as above in an Outside Room from the Other Events Group. Event Other

Ending the Game when there are no more lives.

There is an event that happens when the lives variable goes to or below zero, this is the No More Lives Event.

Open your character and Add the No More Lives Event from the Other Events Group Event Other

In this event, put what actions you want to happen when the character runs out of lives, this could be taking them to a game over screen or back to the title screen.

The object should create the variables as soon as the game starts, you can do this on a Create Event or a Game Start event.

 

Getting Extra Lives

At the moment you can only lose lives, your game will end when you run out. To make things fair, we should add a pick up that awards you an extra life.

First, you need to create a new object, call it, o_pickup_life and give it a sprite.

Now we need to make it give an extra life when it collides with the character.

Add a Collision Event with the Main Character. Event Collision

Add the Set Lives Action Action Lives Set
Set the New Lives as, 1 and tick relative, this give you one extra life.

From the Main1 tab, add the Destroy Instance ActionAction Destroy Instance
This will get rid of the life after you collide with it.

There is a tutorial on how to gain an extra life when you pick up a certain ammount of coins on the site if you want to use that to give the player an extra life as well.

 

Drawing the Lives on the Screen

Now the game has lives, the next thing we need to do is show the user how many they have left. Drawing onto the screen is all done with an object, this object only does the display and nothing else.

Create a new object and call it, o_display, don't give it a sprite.

Add the Draw Event. This is event is used to draw onto the screen. Event Draw
Draw Actions will ONLY work in a Draw Event.

There are two actions to draw the lives on to the screen,

Draw the Number of Lives Action Lives Draw Image

This draws the number of lives in text, you can give this a caption if you want.

Example Lives Number

Draw Lives as an Image Action Lives Draw Image

This draws the number of lives as a sprite, sort of like how it's done in Zelda.

Example Lives Image

 

Drag in either one of these Actions from the Score tab.

On the settings menu, put in what position you want to draw the lives at on the screen.

Example Draw Lives as Image Settings

In this example I have put 10x and 10y, this will draw it at the top left of the level.

If you are drawing lives as an image, you have to also select what sprite to use to represent one life.

If you are using a camera, please read this tutorial or your lives won't show when you scroll the level.

 

Download working example

var_lives.gm6