GameMaker custom health bar question.

I'm attempting to make a health bar that isn't constructed or "deconstructed" in visual increments of a rectangular reference... My heath bar would begin (full) as a complex design which would be subtracted from and later added to with the unique segments that make the graphic. My only idea here to get this to work would be to have as many sprites made in all of the possible health states; but that would seem very difficult code-wise due to so many possible exchanges of health conditions in gameplay. Any thoughts on this?
thx
 
I'm attempting to make a health bar that isn't constructed or "deconstructed" in visual increments of a rectangular reference... My heath bar would begin (full) as a complex design which would be subtracted from and later added to with the unique segments that make the graphic. My only idea here to get this to work would be to have as many sprites made in all of the possible health states; but that would seem very difficult code-wise due to so many possible exchanges of health conditions in gameplay. Any thoughts on this?
thx
What did you have in mind in terms of appearance?
I'm assuming you don't want to use the built-in draw_healthbar function.

You could always just make one sprite and then change the color or scale it through code.

For example, if you have a green bar that you want to turn red and also get smaller, then you would use image_xscale for a horizontal bar and image_yscale for a vertical bar. If you needed to change the x and y values, you'd simply change those.

I'd just use draw_sprite_ext( sprite, subimg, x, y, xscale, yscale, rot, colour, alpha ). That way you'd just have to make one sprite and then change it to your liking through code.
 
I don't want to alter the built-in health bar. I have a graphic that I need to cut up into about 80 pieces with each piece representing a different level of health. So I will be drawing to gui, but I need to figure out the command line... I use DnD btw.
 
You'll need a for loop.
Code:
var h_percent = hp/hp_max;
current_healths = round(80*h_percent);
for var i=0;i<current_healths;i++) {
   draw_sprite(spr_health_part,0,x+(i*32),y);
}
That's one way to accomplish it.
 

Let's Clone

Member
linear interpolation works really well for scaling health bars. Look up lerp() in the dox and come back with any questions if you think that will be a potential option for you :)
 

TheouAegis

Member
Can you post a mock-up of what you're intending? 80 sprites might work well enough, but we may be able to help you get it down to 4 or 5; you never know.
 

Psycho_666

Member
I get what you want.
If I understand correctly your goal is to have something like segmentation of the health bar, let's say you have a robot. You want each part to be represented and then if the part is swapped with different one or a new one is added, that will change the entire appearance of the helthbar. Something like the original starcraft. Like that photoss zealot, that have both legs and his head injured. But you want it representing the real damage taken in game.

Well, the only way I can see that happening is each part having its own health bar and drawing a lot of different images. In that case there ain't one code fits all. SC1 just did random damage representation, but you want yours to be accurate, so you will have to do the work. No real way arround that.
 

Attachments

Firstly, thank you for the help, and happy holidays to you and yours (everybody).:) Here is an example of what I'm trying to do:
spr_Example.png
Although the healthbar I'm making is more complex, this mockup is the basic idea. I won't need different layers to create it, just different sprites, each representing a given state of health. I've found an example of some code that may work, provided that I change up coordinates, etc. The beginning of this video shows this guy's code solution in his GUI tab:
I just need to find a way to "trigger" a GUI, and to have my player's health sync with the triggering. In the video, the guy says he has a "data manager object" which contains player's variables, and a "GUI object" which contains different events...
How do I trigger the GUI? Hoe do I get that to sync with the health of the player? I've tried doing what he's done and it is not working.
RefresherTowel: Thanks so much. I don't know where or how to apply the code you offered. Abrexas: I read through that info and it was over my head (not sure where or how to apply that. Psyco_666: I apologize for my confusing description at the beginning of thread... I should have posted this graphic in the beginning. There is "visual segmentation," but each and all would be handled artistically in each sprite.
What thoughts do people have regarding this code in the video? I've chosen to explore it because it makes more sense to me —I'm a DragNDropper for the most part. thx
 
Last edited:

Psycho_666

Member
Each one of those states in the mockup are their own sprites though
They can easily not be...
Since this is just a visual representation you can just draw one image over another one and the second one will be hidden. You draw one sprite for the full healthbar and then stretch the second sprite over the full bar to cover parts of it. You will be essentially working with two sprites.
 

HayManMarc

Member
Another way to produce that mockup could be just to draw the face sprite whole, then draw the red rectangle at the appropriate size over the top of it.
 
Psycho_666 & HayManMarc: I'd be using 80 to 100 sprites to represent these many states of health level. My actual graphic will be too complex to simply stretch a sprite a solid color over the "positive" or "full" part of sprite due to some artistic aspects. I don't mind making 100 sprites as long as it is for the betterment of my visual goals, I just need a solid way to code these so that my player somehow keeps record of health and then triggers the appropriate draw GUI event. If there were a way to do this in DnD, I'd be thrilled. How does anything happen when a player's health changes, is what I'm saying... I'm relatively new to programming. As much as I respect the advice, it is often suggested that I learn GML. My difficulty is that I can never see it "as applied" unless I watch a video.
 
Post the actual graphics you want to use and let us have a think over what might be the best way to achieve it. I'm inclined to agree with @TheouAegis, I think you're overthinking what it is that you're trying to do and making it seem more complex than it is, draw_sprite_part will probably be all you need to get what you're aiming for (possibly a loop as well, depending on the complexity). Give us the actual graphic and the actual in-game scenario that you're aiming for, rather than outlines of it and links to other resources.
 
Post the actual graphics you want to use and let us have a think over what might be the best way to achieve it. I'm inclined to agree with @TheouAegis, I think you're overthinking what it is that you're trying to do and making it seem more complex than it is, draw_sprite_part will probably be all you need to get what you're aiming for (possibly a loop as well, depending on the complexity). Give us the actual graphic and the actual in-game scenario that you're aiming for, rather than outlines of it and links to other resources.
I'm not really comfortable with posting/sharing any actual assets (no concern of theft or anything like that, it's just a sensibilities issue I would say). The in-game scenario would be a persistent GUI (for appearances, at least), as each segment will be drawn whenever the health situation calls for it. I hope this doesn't put anyone off.
 

TheouAegis

Member
A little picture you showed us depicts a Sprite being drawn in full when your health is 100%, been missing part of the right when your health is lower, then missing more the right when your health is lower, and so forth. That is achieved in one simple line with jusrlt one sprite using the function draw_sprite_part().

If the Starcraft reference went over your head, then clearly you're not dealing with something as visually complex as that, so that leaves majority of us to believe that you are overcomplicating things drastically. lol
 
A little picture you showed us depicts a Sprite being drawn in full when your health is 100%, been missing part of the right when your health is lower, then missing more the right when your health is lower, and so forth. That is achieved in one simple line with jusrlt one sprite using the function draw_sprite_part().

If the Starcraft reference went over your head, then clearly you're not dealing with something as visually complex as that, so that leaves majority of us to believe that you are overcomplicating things drastically. lol
I couldn't say that the example graphics are missing the face as the health goes down and red takes over because that perceived emptiness would have to blend with what is left in a way that could not be achieved via code (at least in the case of what I was working with). It is most likely that I've overcomplicated this idea itself. I'm going to be trying something simpler with the healthbar... I think that if I get better at programming in the future, I'll push for this idea (or one similar), but right now I should apply the best visuals where I'm confident. Thanks for the input, to all.
 
I just realized that I have been making this more complicated —the "how" of it! I've been sittin here for 15 hrs and it was right in front of me... The problem is that I've been seeing the healthy image on top of the empty color moving from left to right, when all that would need to happen is the reverse: The health-emptying color could be on top and move from right to left. I would just need to blur the leading edge and make it partially transparent to get the effect I'm after. jeeez louise, I'm sorry about all of this. I was about to go to bed when it hit me. It hit me because I was going to give up on the idea, but that must have calmed me down enough to see the issue.
Alright, so there's this built-in healthbar feature to GMS2... I just need to switch things around. I can't tell you how many times this kind of thing has happened in this project. I have to get my middle-aged self to bed.
 
Top