ID:1971401
 
(See the best response by Ter13.)

mob
if(Health>=50)
icon="50%"


This isn't the full set of codes, but this should be enough to work on its own. However it's not. I feel like there's something with vars that don't really work fluently in Byond.
That shouldn't even compile, as icon can only be assigned a file. You should be using icon_state instead. Also, you can use <dm> tags for syntax highlighting.
Well, for a start (mind you, I don't know the exact terms), the icon variable either needs to be a reference to an icon datum (/icon) or a file (most common). Either approach, I'm pretty sure you have to reference a file with single quotes ( ' ) so that BYOND knows it's a file that should be stored in your resource file. Technically, you could use file(), but that's neither here nor there.

icon = 'Player.dmi'


With that said, I assume you aren't actually wanting to alter the icon, but instead the icon_state. The only reason I gave that long drawn out introduction into icons, is because it is used very improperly in the given code.

With all that said, I think this would fix your problem.

if(Health>=50)
icon_state = "50%"
Sorry, I meant icon_state, not icon. I'm a bit tired out and about to head to my bed. I just load up the program and I got this error. empty type name (indentation error?) There was a different error with this earlier, and maybe when I boot my computer up again it'll be gone. Who knows. Though ultimately it's like:

mob
if(Health>=50)
icon_state="50%"
if(Health>=25)
icon_state="25%"
if(Health==0)
icon_state="0%"

This should work, shouldn't it?
Umm.... Well, the indentation error is due to the lack of indentation for your if statements. Furthermore, your inequalities statements see, to be backwards (pretty sure you meant <=). Lastly, not sure why you have the random mob there.

if(Health <= 50)
icon_state="50%"
if(Health <= 25)
icon_state="25%"
if(Health == 0)
icon_state="0%"


Also, please surround your code with the <dm> codes </dm> tag when posting. It makes it look so much better in the forums, and makes it easier to read/help.
In response to Ss4toby
I think your problem is resolved.
But i think methods like this:
if(Health <= 50)
icon_state="50%"
if(Health <= 25)
icon_state="25%"
if(Health == 0)
icon_state="0%"

aren't good method.
i think somethink like this, its easier.
icon_state="[Health]%"// now icon_state has value of your health.

But here you can have problems, if your HP will be more than 100... like 101. So now i can show you how to set percent of some numbers.

var/thisPercent=round(min(100,(src.HP/src.MaxHP)*100))
icon_state="[thisPercent]"// now icon_state has value of your health in percent hp/maxhp.


Why i use min() proc?




min proc

Format:
min(A,B,C,...)

Returns:
the minimum of the arguments.

Example:

usr << min(1,2,3)

This example displays 1.

If a single argument is specified, this is expected to be a list and the minimum item from the list is returned. Items to be compared may be numbers, text strings, or null, but numbers and text strings may not be mixed.


Sorry for my poor english.
Best response
A useful function for this:

max(min(1,round(cur/max*DIVISOR)/DIVISOR),0)*100


cur is the current stat's value.
max is the current stat's maximum value.
DIVISOR is the number of divisions the healthbar goes into. For instance, one image for every 10% would be 10 (100/10). One image for every 5% would be 20 (100/5), One image for every 2.5% would be 40 (100/2.5), and one image for every 1% would be 100 (100/1).

The min/max keeps it from going below 0 and 100%.

If you know your divisor ahead of time, you can simplify this equation:

divisor of 20:
max(min(20,round(cur/max*20),0)*5


divisor of 100:
max(min(100,round(cur/max*100),0)


divisor of 10:
max(min(10,round(cur/max)*10),0)*10


Or, if you have DM.Stdlib installed:

clamp(floor(cur/max)*10,0,10)*10
Mmmm, I'm still trying this out, but I'm still getting errors. I like seeing what you both put down. They're definitely an interesting and more complex version, but right now I'm just trying to test various icons related items. So, I'm wondering if there's a problem with the Health var, because this time I am getting a duplication definition with the empty type name. I'm not completely sure how to make indentation appear within the box, but I'll try.

mob
var
CuMaxHealth=100
Health=100//CuMaxHealth

/*mob
icon='GA.dmi'
if(Health<=50)
icon_state"50%"
if(Health<=25)
icon_state"25%"
if(Health<=0)
icon_state"0%"*/


mob
icon='TestGA50.dmi'
if(Health==50)
icon_state="50%"
Statements, such as of statements, generally need to be within a function or a method on most languages, including DM. For this to work, you need a method to update their appearance when they are damaged.
So, I guess I'll have to do something like:
mob
proc
HealthCheck(mob)
if(Health<=50)
icon_state="50%"

The only concern is that wouldn't this take a bit to process this? I guess I could have this proc whenever the character is attacked, but I would think it's possible to have something less consuming.
I would not do it that way, as it violates separation of concerns. It should be handled in a method that is called when HP changes, but not within the method itself.

There are two reasons for this, both clear of your consider a subclass. If this subclass handles a loss of HP differently than its superclass, but not its change of appearance, then your shouldn't have to modify both. Conversely, if it's appearance changes differently but not how it loses HP, then you shouldn't have to modify both.
Hmm, it seems to be working now, and I will update it more. However the problem seems to be how to make the movement state work with the rest of the 'GA.dmi' icon.