Favorites Posts ID:155391
 ID:155391   Jul 17 2011, 10:12 am Hi,I'm currently trying to make a health bar for my HUD , in an RPG game, unfortunately, I can't go If(hp == 90) icon_state = "90hp" , because at a high level, 90 would be VERY low. So, does anyone know how to do percentage?
 #1 Jul 17 2011, 11:25 am You wouldn't need to put the ,1 at the end of the round(). Round() defaults to 1. ;)
 #2 Jul 17 2011, 11:42 am so, would I just make icon states called 90 70 and 50 and so on?
 #3 Jul 17 2011, 12:21 pm
 #4 Jul 17 2011, 12:25 pm In response to Oasiscircle (#1) Oasiscircle wrote: You wouldn't need to put the ,1 at the end of the round(). Round() defaults to 1. ;) Actually, round(n) is equivalent to a floor() function, whereas round(n,1) will round to the nearest whole number. For simple displays like this, it probably doesn't matter, but it's important to keep in mind when doing heavier calculations. round(0.7) = 0 round(0.7,1) = 1
 #5 Jul 17 2011, 12:31 pm In response to DarkCampainger (#4) Oh, thanks for clarifying that! I never knew that.
 #6 Jul 17 2011, 12:35 pm In response to BeignetLover (#2) If you wanted the percentage to round to the nearest 10, you'd put round(X, 10) where X is the percentage calculation (current/max * 100).
Lige wrote:
BeignetLover wrote:
Hi,I'm currently trying to make a health bar for my HUD , in an RPG game, unfortunately, I can't go If(hp == 90) icon_state = "90hp" , because at a high level, 90 would be VERY low.
So, does anyone know how to do percentage?

 ```> icon_state="[round(health/maxhealth*100,1)]%"> ```

It's giving me crap about constant expression, which I don't even know what that is, and it recognizes "HP/maxHP" as a full variable ;( Anyone know why?
In response to BeignetLover (#7)
It's because you're attempting to set the variable to an expression at compile time. Instead of this:

 ```obj health_bar icon_state="[round(health/maxhealth*100,1)]%" var health maxhealthmob proc/take_damage(n) hp -= n health_bar.health -= n ```

You need this:

 ```obj health_bar proc/set_health(health,maxhealth) icon_state="[round(health/maxhealth*100,1)]%"mob proc/take_damage(n) hp -= n health_bar.set_health(hp, max_hp) ```