Enemies with Health

Before you start, you will need either a Maze or Platform engine with a character that can attack.
You will also, and obviously, need at least one type of enemy.

Download the the Basic Platform Shooter engine here

 

Giving your Enemy Health

We will keep track on an enimies health using a variable, with varibales, before we can use it, we must create it when the object is created.

Open your Enemy object and Add or Open the Create Event Event Create

From the Control Tab, add the, Set Variable Action Action Set Variable

This new variable will keep track of how much health the enemy has.

Example Set Var Hp

Set the variable name as, hp
Set the value as, 5, or how ever much health you want to give it.

You can't name the variable, health, because that variable is already used by game maker to keep track of the player's health.

Now we have made a variable to store the enimies health, we have also set it, now, we need to reduce it when the enemy is hit with an attack, in this example, the bullet.

 

Reducing the Health when Hit with an Attack

Create a collision event with the bullet or weapon that will hurt your enemy. Event Collision
In this example, i'm using a bullet.

From the Control Tab, add the, Set Variable Action Action Set Variable

Set the variable name as, hp, because we want to change the hp
Set the value as, -1 and tick relative, this will take 1 away from the hp value

If you are using a bullet in the collision,

From the Main1 Tab, add the, Destroy Instance Action and Select Other Action Destroy Other
This will delete the other object invovled in collision, which is the bullet.

 

Checking the Health and Destorying the Enemy if No Health

At the moment, your enemy loses health when it is attacked. We want to destory the enemy when it has no health left. We can't use the No more Health Event because this is for when your main character has no more health, instead, we have to check the hp variable we created on the Enemy.

Still in the Collision event with the bullet or weapon,

From the Control Tab, Add the, Check IF Variable has a Value Action Action IF Var has Value

Example IF hp is Less Than 1

We want to check if the hp is less than 1, meaning there is none left.

Set the variable as, hp, because we want to check how much hp is left.
Set the value as 1 and the operation as smaller than.

This will either be true or false, if it is true, we want to delete the enemy object.

After the IF Variable has a Value Action, from the main1 tab, add the Destory Instance Self Action Action Destroy Instance

 

The collision event with the bullet should now look like this,

Collision with Bullet

 

This will take 1 away from the hp and then destroy the bullet that hit the enemy. Then, it will check if the hp is less than 1, if it is, it will destory the enemy.

 

Download working example

enemy_health.gm6