ID:155391
 
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?
You wouldn't need to put the ,1 at the end of the round().
Round() defaults to 1. ;)
so, would I just make icon states called 90 70 and 50 and so on?
In response to Oasiscircle
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
In response to DarkCampainger
Oh, thanks for clarifying that!
I never knew that.
In response to BeignetLover
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
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
maxhealth

mob
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)