ID:1430849
 
(See the best response by Mask of Illusion.)
Problem description:
Hello guys, I am almost 100% sure that I've come across the solution to this problem before, but I can't for the life of me get hold of the topic again, so I ask of my fellow BYONDers to give me a helping hand. I decided to try my hands at using HUDS instead of interface after I found a sweet looking one at OpenGameArt, anyway. I am currently facing a issue updating my HP bar I don't know how to get around. It's pretty basic:

mob/proc
Update_Health()
var/HP_Percent = round(src.Health/src.MaxHP*100)

if(HP_Percent>100)
//Precautions
HP_Percent = 100


if(HP_Percent<=0)
//Add death system
HP_Percent = 0

for(var/obj/HUD/Healthbar/HP in src.client.screen)

HP.icon_state = num2text(HP_Percent)


The problems with this system are quite obvious, the most obvious one being - What happens if you don't have 100 icon_states(ridiculous)- and being one of the issues I am currently faced with. I have each icon state in 10 point fragments, which leaves A LOT of room for errors(like the HP Bar disappearing because there is no such icon_state)

Now, as stated previously, I know I have come around a topic or library which addressed this issue.. If anybody could help me find it and link me to it , that'd be great. Or if you could discuss with me another different approach I could take, because I know there must be a way to do this accurately without having to create a bizarre amount of icon_states.

-Thanks
Best response
Based on how you've described it would using the crop() process on the icon to show the amount be feasible? Obviously if you have a special effect for your health being at 100% (like it glowing for example) you'd need to make a separate icon.

Alternatively if you wanted to keep it so that your values were always a multiple of 10 then you'd need to use round() in there to round it to the nearest 10 before setting the icon state.
Oh man I can't believe I didn't think of that, thanks for the reply. I got it easily fixed by changing

var/HP_Percent = round(src.Health/src.MaxHP*100)


to:
var/HP_Percent = round(src.Health/src.MaxHP*100,10)
If the icons are just text, you should be using maptext instead.