ID:270157
 
I want to put a health bar into my game but have very little knowledge on how to do it. I know that I need an icon with different icon states for it. I got that. Now how do I make the icon show up as the health bar? This brings up another question. How do i incorperate icons into a stat panel? I don't plan on putting the health bar over the character, but having it in a stat panel.
Make a new object, set the icon's object to the health bar icon, and put the object in the statpanel.
client
var/obj/health_bar/health_meter
New()
.=..()
health_meter = new
Stat()
statpanel("Stats")
stat(health_meter)

obj/health_bar
icon = 'health bar.dmi'

You could set the health_meter's icon_state in the Stat function if you wanted to take the easy way out, but I would suggest you change it after every time the client's mob's health is changed.
In response to Loduwijk
How do I make a proc that will update the health bar depending on how much health the user has (I want it to check every time the user gets attacked and takes damage).
In response to Clark Kent
Nevermind. I figured it out
In response to Clark Kent
If the icon states for the health meter are numbered 0 or 1 through the number of states, then you can just use a formula to grab the appropriate one. health/max_health*max_state. max_state is, of course, the highest numbered icon state. So, if you use an entire icon and have a meter go up or down by a pixel every time, you would probably have icon states numbered up to 32. In such a case you would do health/max_health*32.
mob/proc/update_meter()
if(!client)return 0
client.health_meter.icon_state = num2text(health/max_health*32)

Then you could call it wherever health is changed.
mob
verb
attack(mob/M in oview(1))
M.damage(1)
first_aid(mob/M in oview(1))
M.heal(1)
proc
damage(N)
health = max(0, health-N)
update_meter()
heal(N)
health = min(max_health, health+N)
update_meter()
Bump(atom/movable/M)
if(istype(M, /obj/spiked_trap))
damage(1)
turf/magic_pool
verb/drink()
set src in view(1)
usr.heal(1)
In response to Clark Kent
Now I have a new problem. It seems the way it's set up it's affecting everyone (modifying everyones health bar when someone gets attacked). The code is as follows:
client
var/obj/health_bar/health_meter
New()
.=..()
health_meter = new
Stat()
health_meter.name = "[usr.health]%"
statpanel("Status")
stat("Name:","[usr.name]")
stat("Health:",health_meter)
stat("Chakra:","[usr.chakra]/[usr.totalchakra]")
stat("Title:","[usr.title]")
stat("Strength:","[usr.strength]")
stat("Defence:","[usr.defence]")
stat("","----------------")
stat("Money:","$[usr.money]")

obj/health_bar
icon = 'health bar.dmi'
icon_state = "full"

mob/proc/HealthCheck()
for(var/obj/health_bar/health_meter)
if(usr.health > 90 && usr.health <= 100)
health_meter.icon_state = "full"
if(usr.health > 80 && usr.health <= 90)
health_meter.icon_state = "90%"
if(usr.health > 70 && usr.health <= 80)
health_meter.icon_state = "80%"
if(usr.health > 60 && usr.health <= 70)
health_meter.icon_state = "70%"
if(usr.health > 50 && usr.health <= 60)
health_meter.icon_state = "60%"
if(usr.health > 40 && usr.health <= 50)
health_meter.icon_state = "50%"
if(usr.health > 30 && usr.health <= 40)
health_meter.icon_state = "40%"
if(usr.health > 20 && usr.health <= 30)
health_meter.icon_state = "30%"
if(usr.health > 10 && usr.health <= 20)
health_meter.icon_state = "20%"
if(usr.health <= 10)
health_meter.icon_state = "10%"
if(usr.health == 0)
usr << "<font color = red><b>You were beaten to the brink of death! Luckly you escaped just in time, but you have to be more careful!"
usr.loc = locate(77,77,2)
usr.health += 100
health_meter.icon_state = "full"
In response to Clark Kent
The for(var/obj/health_bar/health_meter) is what's doing it to you. Looping through them with a for-loop is going to loop through all of them that exist. What you want in that situation is just client.health_meter.