SoulHow to make two health bars (Game Maker)

Hey there, and welcome to another SoulHow Game Maker assistance guide. This time around it won’t be so darn long, because I’m going to explain how to create two health bars for your Game Maker game. It seems impossible at first, but it can certainly be done. Here’s how.

Step 1: Setting up the health variables

First, you’ve got to understand variables. For this, you could choose to read the SoulHow article on learning GML, but if you’re already using the built-in GM variable health, you probably already understand, at least a little bit. Basically, a variable is like a box which you put numbers or words into. health is a variable which holds a number that corresponds to the amount of health some object has left (maybe a player?) So what we’re going to do, is make our own variables to store health. We want to do this instead of using GM’s health variable because then we get total control.

We’re going to make these variables global, so all objects can use them. So make a brand new object, and call it “objHealthController”. Add a create event to the object, and in the create event place this code:

Execute a piece of code
global.hp1=100;
global.hp2=100;

Now we have two different variables, one for the first hp and one for the second hp. This could for example be used for two different players in a game, or for a player and a boss, etc. Use these variables just like the regular health variables, except you can’t use the “set the health” action. You have to use “set variable” and for the variable name, type either “global.hp1″ or “global.hp2″. For example,

Set variable global.hp1 relative to -1

The above action would deduct 1 from the first hp variable. Maybe player 1 got hit or something. If player 2 were to get hit, you’d do the same thing except type “global.hp2″. Should you wish to do something when the player runs out of HP (dies?) you’d use the “test variable” action:

If global.hp1 is smaller than 1
Start of block marker
[Put your actions for what should happen when the first health is gone here]
End of block marker

We test if global.hp1 is smaller than 1, because that includes 0 and any negative numbers like -1. Again, for the other player’s health, just change “global.hp1″ to “global.hp2″.

Step 2: Drawing the healthbars
Ok, now we have to actually show each health bar on the screen. We are going to do this using actions, but it’d be easy to change it to code. Here we go:

In draw event of objHealthController (the “set the color” actions are optional but if you set each to a different color then the rectangles will be different colors as well):
Set the color
Draw rectangle
x1: view_xview[0] + 16
y1: view_yview[0] + 16
x2: view_xview[0] + 16 + global.hp1
y2: view_yview[0] + 16 + 24

Set the color
Draw rectangle
x1: view_xview[0] + 16
y1: view_yview[0] + 16 + 24 + 8
x2: view_xview[0] + 16 + global.hp2
y2: view_yview[0] + 16 + 24 + 8 + 24

As you can see, these healthbars will stick with the view, so you don’t have to worry about that either. The numbers we add to the view_* variables are just offsets; for example, each healthbar is offset 16 pixels from the left side of the view (”view_xview[0] + 16“). The global.hp1 and global.hp2 are there so the width of the drawn rectangle changes with those variables.

And there you go. Pretty easy, eh?

That concludes another SoulHow guide. Feel free to use any of the code, etc. posted here in your own game. I’m also interested in comments/criticism/suggestions for this article, so please, all comments welcome.

If you have a technical question, such as “How do I do this” or “this is not working right” (relative to something with your Game Maker game after reading this tutorial), please head over to this community(SoulRed Productions Community) and post in the “SoulScroll discussion” forum.

8 Responses

  1. Heya! Thanks for the tutorial, it’s really clear! ^_^ If I want to make a healthbar which has a background, do I just draw two rectangles on top of each other? Can I set the depth of the rectangles? ^_^ Thanks for the help!

  2. Nevermind, you just draw the bottom rectangle first. ^_^ I just tried it out and it worked! Wooo! Thank you soooooooooooo much!!

  3. soulred12 you are mad, i looked for ages to make healthbars and you fixed it, although i dont quite understand how to get the bars in the top left hand corner. can you help?

  4. You are my #1 source for game maker tutorials! You rule! w00t!

  5. BTW, could you make a guide on how to make a sword swing and do damage? Thanks, dude!

  6. Hi, thanks for the tut… Works perfectly.

  7. Guys, everything you need to know is in the article. If you have any questions, just read the article over again, because there’s sections about resetting health, doing things when health reaches 0, etc.

  8. Hey thanks for this its what i really needed. if you want to see the game its on yoyo games called DWMMBEKW V7.11.3. Theres an older one on there right now butt ill change it later
    Thanks again

Leave a Reply