Trying to get health bar to normalize a value

J

JTrocks55

Guest
So here is the problem I am having. I watched this video: GameMaker - Custom Healthbars
In the video, he explains how to make a healthbar. His healthbar went off of a health value that was maxed at 100, so he showed that you need to draw the healthbar as your health value divided by 100, making it so that the xscale of the sprite equals anywhere from 0 to 1, because it is 0 to 100 divided by 100.

My problem is that in my game, your health starts at 50 (50 divided by 100 is obviously going to be .5, so the health bar shows half health), then you can also upgrade your max health between waves. So what I am looking for is a way to normalize any health value to be 0 to 1. Here is the code that handles the drawing of the healthbar:

Code:
draw_sprite(spr_healthbarbg,0,obj_player.x - 50,obj_player.y - 68);
draw_sprite_ext(spr_healthbarinner,0,obj_player.x - 50,obj_player.y - 68, global.hp/100,1,0,c_white,1);
As you can see, global.hp/100 won't work in my case.

So, any ideas on how I could get this fixed?
Thanks in advance! :)
 
J

JTrocks55

Guest
divide by 50?
Well the thing is, if your health starts at 50, it will display correctly, but then you upgrade you health to say, 70. That is the problem, the max health value changes throughout the game. So I cannot use one value to divide by. Thank you though!
 

jazzzar

Member
ok put a variable in create event name it max_health, give it an intial value of 50 then after upgrades increase it and change this line of code
draw_sprite_ext(spr_healthbarinner,0,obj_player.x - 50,obj_player.y - 68, global.hp/100,1,0,c_white,1); to this :
draw_sprite_ext(spr_healthbarinner,0,obj_player.x - 50,obj_player.y - 68, global.hp/max_health,1,0,c_white,1);
 
J

JTrocks55

Guest
I am so sorry, I think I just figured it out, and it was so simple! I should have tried more things before posting. I will post back if this continues to work or not.
 
J

JTrocks55

Guest
ok put a variable in create event name it max_health, give it an intial value of 50 then after upgrades increase it and change this line of code
draw_sprite_ext(spr_healthbarinner,0,obj_player.x - 50,obj_player.y - 68, global.hp/100,1,0,c_white,1); to this :
draw_sprite_ext(spr_healthbarinner,0,obj_player.x - 50,obj_player.y - 68, global.hp/max_health0,1,0,c_white,1);
Ah see that is what I just figured out! I was responding with that when you said it! :D
 
J

JTrocks55

Guest
Thank you for the help though! I have never seen a forum where people respond so quickly! Unreal engine forums can take weeks for someone to respond lol. Thanks again!
 
Top