Home > SoulHow > SoulHow to make two health bars (Game Maker)

SoulHow to make two health bars (Game Maker)

June 4, 2008

NOTE: Comments are locked. I no longer answer questions about the Game Maker tutorials on this blog; I suggest you take any questions to the Game Maker Community. For more info, view the FAQ page.

Skill Level: Novice user (4)

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 as we have seen in previous articles, anything is possible with a little wizardry.

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 to make two different health variables whereas GM only has a single built-in health variable.

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 variable, 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 (rather than if it’s equal to 0), because that includes 0 and any negative numbers like -1.  What if, for example, the player is at 10 health and something deducts 20 from it?  We get -10; and if we only tested for health being equal to 0, then nothing would happen because -10 is certainly not equal to 0.

And 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; you can pick whatever colors you want):
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

Due to all those “view_*” variables, 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 as those variables change (which gets us the effect of the healthbar shrinking as each hp shrinks).

And there you go. Pretty easy, eh?  Play around with some of the values in the draw_rectangle actions so you can get a feel for what they really do.

Anyhow, that concludes another SoulHow guide. Feel free to use any of the code, etc. posted here in your own game.

If you enjoyed this article, please consider checking out the rest of the blog.

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 the Game Maker Community and ask there.  I can’t answer any such questions in this blog and the members at the GMC will be more than capable to help you.  For more info, check out this blog’s FAQ page.

Advertisements
  1. September 8, 2009 at 4:57 pm

    Thank you so much!!!

  2. harry
    May 8, 2009 at 1:25 pm

    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

  3. soulred12
    May 2, 2009 at 10:45 am

    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.

  4. Dan
    April 27, 2009 at 11:44 am

    Hi, thanks for the tut… Works perfectly.

  5. Hi
    January 27, 2009 at 6:49 pm

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

  6. Hi
    January 27, 2009 at 6:46 pm

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

  7. jjj
    November 11, 2008 at 6:42 am

    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?

  8. Nina
    August 11, 2008 at 8:26 am

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

  9. Nina
    August 11, 2008 at 6:31 am

    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!

  1. July 1, 2016 at 3:36 pm
Comments are closed.
%d bloggers like this: