ID:1210928
 
Keywords: bar, health, help
Code:


Problem description:
Is there a way to take a health bar and make the bar lose health with out having icon states such as 100,90,80,etc? If their is a way to do so with out having the many icon states please show me how.
return round(( health / max_health )*62,1)

62 being the amount of icon_states you have & 1 affecting how it's rounded up.
You technically could have a 2px line as icon_state and with a loop, add these lines based on a percentage, also with a 2px offset on the X axis.
Thing is I am asking if theres a way I can do it without having icon_states. @Kozuma
In response to ImmeasurableHate
ImmeasurableHate wrote:
Thing is I am asking if theres a way I can do it without having icon_states. @Kozuma

You:"If their is a way to do so with out having the many icon states please show me how."

Fushimi's idea is what you want then.
Its not going to work with how my health bar looks =/
Then use icon_states, you don't need one for every small change in value. If you wanted 10 icon_states use
round(( base / max_base ) * 10,1)
You could use that same 2x icon and have the game generate icons using the Blend() and icon() procs a single time into world variables for later use. A single list of generated icons, each [n] place could be their direct value too.

Best of both worlds, you only have a single icon to look at but get the same outcome with I guess less overall ram usage?
If your health bar is just one single color, you can use scale() to change the size. I implemented something like that in a demo somewhere. You just increase the width of the icon depending on your health, and the max health.
In response to Jittai
I actually use icon.Insert() to insert [n] icons into a single icon with "[n]" icon states. I haven't tested yet the performance difference between this and a list of icons.
In response to Kaiochao
I haven't done it myself, but I imagine aftr the initial generation the only ussage that occurs when you use

/obj/Healthbar.icon_state = generated_icons[src.Health]

this really only works easily if you're health_bar is 1 tile size though. Otherwise you'll have to do some funky stuff like checking/forcing parts of your bar to look full if it goes over.
I'm putting myself out on a limb here, but I wanted to point out that generating your healthbars by using 100 objects is a horrible idea. The reason for this is simple: network traffic and scalability. Even if you only made one set of 100 objects, it's still going to take longer for the client screen lists to be sent over the network, and it's going to eat up more memory, and more cpu due to list management.

As for generating the healthbars using icon procs... well, I can only agree with this method if it's being done before the game is ever run, and the results are being included in the project permanently. Otherwise, it's another waste of resources and processing power.

The best solution when discussing resources in general, hands down, is using multiple, pre-made icon states.
Optionally, there is actually a way to do this effectively if your healthbars don't lay on the map element using labels, with the background image set to the healthbar image, and the mode set to tiling. Just resize the label to decrease the visible portion of the bar. But again, you don't want to do this over the map element due to rendering artifacts.