• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Help with my custom healthbar please (SOLVED)

J

jacksu456

Guest
I have a little problem with a custom healthbar that i made, its not the simple square healthbar, its like a gun symbol, so i made a 100 animations of the healthbar going down in a single sprite because my player has 100 health points, so, i just want it to go down every time an enemy collides with the player (in the animation order), but i cant seem to figure it out, i already made the code so that when the enemy collides with me, he hurts me so thats -1 point everytime im colliding with the enemy, but i cant seem to know how to make my bar go down with my 100 animations, im stuck pls help thanks

This is the code i've been doing in obj_hp

Code:
draw_sprite(spr_hp_fill, 1, view_xview[0]+85view_yview[0]+36);

draw_sprite(spr_hp_empty, 1, view_xview[0]+90, view_yview[0]+40);
The fill has 100 sprites more inside it, and the empty thing, is just the hp hud, its put to be inside it, i need to have a way so it can play the sprites 1 by one each time an enemy collides with me because thats 1 point less
 
B

bojack29

Guest
Use gunImageIndex to draw the image index of the gun. Intiialize it to 100 in the create and subtract 1 every time you get hurt
 
J

jacksu456

Guest
Im what you would call a noob or something in this, the gunImageIndex is a variable of the empty hud right? what would i do with it? would i put it like this?:
gunImageIndex = 100;

then what? sorry to bother im pretty new at this
Use gunImageIndex to draw the image index of the gun. Intiialize it to 100 in the create and subtract 1 every time you get hurt
 
B

bojack29

Guest
Code:
//Create event
gunImageIndex = 100;

//Collision event with enemy
gunImageIndex --;

//Draw event
draw_sprite(spr_hpFill, gunImageIndex, 0, 0);
 
T

The Shatner

Guest
Hey jacksu456!
Another way would be to set the image_index of the "revolver HP bar" thingy to be equal to the player hp.
In pseudo-code:
Code:
//CREATE EVENT of the HP object:
image_index = 100; //assuming that the image_index 100 is where you kept the full healthbar
HP = 100;

//STEP EVENT
image_index = HP; //this will show the healthbar exactly as the object's HP
In case that's not what you wanted, can you explain it a little more? Even better, post some screenshots of what you are getting vs. what you want.
 
D

DyingSilence

Guest
If your health is 100, you can use your health variable instead.
 
J

jacksu456

Guest
Hey jacksu456!
Another way would be to set the image_index of the "revolver HP bar" thingy to be equal to the player hp.
In pseudo-code:
Code:
//CREATE EVENT of the HP object:
image_index = 100; //assuming that the image_index 100 is where you kept the full healthbar
HP = 100;

//STEP EVENT
image_index = HP; //this will show the healthbar exactly as the object's HP
In case that's not what you wanted, can you explain it a little more? Even better, post some screenshots of what you are getting vs. what you want.
Well i tried it but it still wont work, im going to show you more or less how im doing it so you can understand. Heres a video:
 
J

jackhigh24

Guest
you video needs you to sign in ??, anyway just search the forum as this has been asked before in the last week or two, you will find us saying to use draw sprite part, this means you will only have two images not 101 as you have now, you just draw the outline sprite and draw the full inside image sprite then every time you take a point away from your player you also take a point or pixel away from your sprite, i think most of the code you need was already in that topic if not we will help you fill in the bits that were missed but go find it in the forum first.

EDIT i think it was called help with drawing custom health bar.
 
J

jacksu456

Guest
you video needs you to sign in ??, anyway just search the forum as this has been asked before in the last week or two, you will find us saying to use draw sprite part, this means you will only have two images not 101 as you have now, you just draw the outline sprite and draw the full inside image sprite then every time you take a point away from your player you also take a point or pixel away from your sprite, i think most of the code you need was already in that topic if not we will help you fill in the bits that were missed but go find it in the forum first.

EDIT i think it was called help with drawing custom health bar.
Its fixed now, if you could watch it i would very much appreciate it
 
T

The Shatner

Guest
Ok. Let's get it started.
First of all, the HP bar drawing: You have a sprite with 100 subimages, and when image_index = 0, the sprite shows full health, right? So, in the object that draws the sprites you should do as follows:
Code:
//BEGIN EVENT:
playerhp = 100; //initialize the variable
//STEP EVENT:
playerhp = obj_player.HP; //Or what you've called the player's hit points
//DRAW EVENT:
draw_sprite(spr_spritename,100-playerhp,x,y); //the image_index will then be set to the player's HP
Your character dies instantly because the collision is constantly happening, so it actually decreases one HP per step while you are colliding with the enemy object.
Solutions to this are creating a "cooldown" timer that only allows damage when it's been a while since you have been hit, or adding a "knockback" effect to the player object in the same piece of code where you put the HP decrease.
 

Tsa05

Member
Hi there,

I created an example for you; you can import the gmz file below into a new project to check it out:
https://dl.dropboxusercontent.com/u/14325048/Game Maker Examples/CustomHealthBar/CustomHealthBar.gmz

If you'd like to preview it working in a browser, here it is:
https://dl.dropboxusercontent.com/u/14325048/Game Maker Examples/CustomHealthBar/index.html

The example uses just 3 images, instead of several hundred!
The trick is to use GameMaker's draw_sprite_part function to draw only a part of the colored bar. In this example, you can change health and armor values, and the green and yellow bars will adjust correctly.
 
J

jackhigh24

Guest
@Tsa05 nice little example there, but you set it up a little wrong, by doing it the way you did you can go into minus by 3 on the health bar.
 
J

jacksu456

Guest
Ok. Let's get it started.
First of all, the HP bar drawing: You have a sprite with 100 subimages, and when image_index = 0, the sprite shows full health, right? So, in the object that draws the sprites you should do as follows:
Code:
//BEGIN EVENT:
playerhp = 100; //initialize the variable
//STEP EVENT:
playerhp = obj_player.HP; //Or what you've called the player's hit points
//DRAW EVENT:
draw_sprite(spr_spritename,100-playerhp,x,y); //the image_index will then be set to the player's HP
Your character dies instantly because the collision is constantly happening, so it actually decreases one HP per step while you are colliding with the enemy object.
Solutions to this are creating a "cooldown" timer that only allows damage when it's been a while since you have been hit, or adding a "knockback" effect to the player object in the same piece of code where you put the HP decrease.
Someone told me too that you could do it with shaders or something like that, that i didnt need to use 100 subimages so the game would lag less, this is the thing that the guy told me on reddit:

"There are ways using surfaces or shaders by which you can draw a sprite onto a mask (like the shape of your health bar), meaning you'd only need at most two sprites to accomplish the effect, which would not only reduce memory usage but would also be way less of a headache to edit.
A quick search on the subreddit yields this post which contains a couple answers on the subject. If you've worked with surfaces or shaders in the past then I definitely suggest looking into ways to pull something like this off, but if you haven't then I wouldn't worry too much about it for now."

Would anyone know about this method?
 
J

jackhigh24

Guest
using a surface or shader is harder and not really needed, you just need one outline image and one fully filled in inner image, use draw sprite part will be so much easier for you and perfect for this kind of thing.
 
J

jacksu456

Guest
Ok. Let's get it started.
First of all, the HP bar drawing: You have a sprite with 100 subimages, and when image_index = 0, the sprite shows full health, right? So, in the object that draws the sprites you should do as follows:
Code:
//BEGIN EVENT:
playerhp = 100; //initialize the variable
//STEP EVENT:
playerhp = obj_player.HP; //Or what you've called the player's hit points
//DRAW EVENT:
draw_sprite(spr_spritename,100-playerhp,x,y); //the image_index will then be set to the player's HP
Your character dies instantly because the collision is constantly happening, so it actually decreases one HP per step while you are colliding with the enemy object.
Solutions to this are creating a "cooldown" timer that only allows damage when it's been a while since you have been hit, or adding a "knockback" effect to the player object in the same piece of code where you put the HP decrease.
Oh and thank you, your method worked, thanks for everything everybody ;)
 

Tsa05

Member
@Tsa05 nice little example there, but you set it up a little wrong, by doing it the way you did you can go into minus by 3 on the health bar.
Looks like this is solved, but I just wanted to toss in a replu about that example I did, in case anyone else should find it; yes, the way I've got it implemented, hp and armor can go to minus 3...or, actually, to minus a ton. It's an example of how to draw partial sprites without needed hundreds of subimages, not an example of how to subtract health (I assume the OP's game is more complex in that area than my example). Naturally, when subtracting from health or armor, you'd check whether you got to a negative number, and you'd prevent that.
 
Top