ID:139746
 
Code:Health bar above mob (Enemy)
mob/verb
Attack()
set hidden = 1
if(usr.Attacking) return
if(usr.frozen == 1) return
usr.Attacking=1
usr.icon_state = ""
spawn(10) if(usr) usr.Attacking=0
flick("Attack[usr.icon_state]",usr) //this can be used to make the character look like their attacking
usr.icon_state = "Combat Stance"
for(var/mob/Monster/M in get_step(usr,usr.dir)) //finds any monsters directly in front of the user
var/damage = src.Str-M.Def //a simple damage calculation
damage=max(0,damage+rand(-1,1)) //make sure damage isnt negative and varry it a little
M.HP-=damage //subtract the damage from HP
M.updateHealth()
M.DeathCheck(usr) //check if you killed the monster


mob
proc
updateHealth()
var/percent=round(HP/MaxHP*100,1)
if(percent>100) percent=100
if(percent<0) percent=0
overlays=list()
//What you need to do is update all your overlays by re-adding them so you can add the new set below... My prefered method to this is to make a different list with all the other overlays and do overlays+=P_Overlays or whever your list is called.
overlays += image('healthbar.dmi',icon_state="[percent]",pixel_x =18,pixel_y=40)
..()
proc/add_hpbars()
usr.overlays += /obj/hudMeters/health_01
src.updateHealth()
obj
hudMeters
health_01
icon='healthbar.dmi'
icon_state="0"
pixel_y=40
pixel_x=18


Problem description: Health bar does appear and does update, but on soms occasions, it suddenly dissapears.

Try changing the layer to MOB_LAYER
In response to Darker Legends
Got compiling errors, saying I cannot change the constant value.
In response to MitC
obj
hudMeters
health_01
icon='healthbar.dmi'
icon_state="0"
pixel_y=40
pixel_x=18
layer = MOB_LAYER
In response to Darker Legends
Still the same problem, dissapears all of a sudden.

Maybe these code snippets provide more information?

mob/proc/DirectDamage(var/SkillName,var/damage) //this proc works alot like the attack verb, except with a preset damage amount
for(var/mob/Monster/M in get_step(src,src.dir)) //finds any monsters directly in front of the user
M.HP-=damage //subtract the damage from HP
M.updateHealth()
M.DeathCheck(src) //check if you killed the monster


obj/Supplemental
Projectile //this will be modded to make up any projectile attacks!
density=1 //make sure it can actualy hit stuff!
var/mob/Owner //somebody had to shoot it right?
var/damage //and we want it to actualy hurt stuff eh?
icon='Projectiles.dmi' //setup its icon
New()
spawn(100) del src //this will automaticaly delete it after 10 seconds, if its still around
Bump(mob/M)
if(ismob(M))
if(src.Owner) //make sure it has an owner! otherwise dont bother
M.HP-=src.damage
M.updateHealth()
M.DeathCheck(src.Owner)
del src //delete it after it hits something
Does the health bar actually have 101 separate icon states, from 0 to 100?
In response to Garthor
No, only 0 to 100, so that'll be 11 states.
In response to MitC
You just answered your own question as to why there is no visible health bar at times.
Consider the possible values of variable 'percent' and compare them to the icon_states.
What would happen if you're setting an icon_state that isn't defined in the icon?
In response to Schnitzelnagler
Schnitzelnagler wrote:
You just answered your own question as to why there is no visible health bar at times.
Consider the possible values of variable 'percent' and compare them to the icon_states.
What would happen if you're setting an icon_state that isn't defined in the icon?

That's not the problem it seems. I made icon states from 0 to 100 and it's still vanishes at some occasions?
In response to MitC
Are you sure you're not setting icon_state to an icon_state that doesn't exist?
Well i looked at this and it helped me do it for my game. But i think i fixed your error by doing this:

mob
proc
updateHealth()
var/percent=round(HP/MaxHP*100,1)
if(percent>100) percent=100
if(percent<100 && percent>89) percent=90
if(percent<90 && percent>79) percent=80
if(percent<80 && percent>69) percent=70
if(percent<70 && percent>59) percent=60
if(percent<60 && percent>49) percent=50
if(percent<50 && percent>39) percent=40
if(percent<40 && percent>29) percent=30
if(percent<30 && percent>19) percent=20
if(percent<20 && percent>9) percent=10
if(percent<0) percent=0
overlays=list()
//What you need to do is update all your overlays by re-adding them so you can add the new set below... My prefered method to this is to make a different list with all the other overlays and do overlays+=P_Overlays or whever your list is called.
overlays += image('healthbar.dmi',icon_state="[percent]",pixel_x =18,pixel_y=40)
..()
proc/add_hpbars()
usr.overlays += /obj/hudMeters/health_01
src.updateHealth()
obj
hudMeters
health_01
icon='healthbar.dmi'
icon_state="0"
pixel_y=40
pixel_x=18


Yeah its a little late but this code helped me a lot. I hope this helps you find the problem. Before my fix the icon state would go to something like 24 or 67 because it was 1 - 100 percent. the way i did it, it goes from 0 to 10 to 20 to 30 to 40 to 50 to 60 and so on, skipping all numbers not divisible by 10. That makes your 11 icon states work so they won't go away at times. Now to test this, before you change it, put your HP at 34 and run the update proc and see if the overlay goes away. Then you will see what i mean.
In response to Dtwon
You may want to change if(percent<0) to if(percent<=0). Right now, it won't update unless their health is less than zero.
In response to Albro1
oh well i already tested mine and it works fine. when its percent<0 it returns to 0 so it won't be a negative. that's all that part of the code is used for ^_^. Thanks tho, It might come in handy for other codes i might do.
In response to Dtwon
It looks like you'd want it to be if(percent < 10) percent = 0. With what you had, if percent was 4 it wouldn't be changed.

If you want to set percent to the nearest multiple of 10, you can use round(). It takes a second argument which lets you round it to the nearest multiple of that. You can just do:

percent = round(percent, 10)
if(percent < 0) percent = 0
else if(percent > 100) percent = 100
In response to Forum_account
Forum_account wrote:
It looks like you'd want it to be if(percent < 10) percent = 0. With what you had, if percent was 4 it wouldn't be changed.

If you want to set percent to the nearest multiple of 10, you can use round(). It takes a second argument which lets you round it to the nearest multiple of that. You can just do:

> percent = round(percent, 10)
> if(percent < 0) percent = 0
> else if(percent > 100) percent = 100
>


Yeah i see what your getting at but in my game the percent is supposed to stay at 10 until it reaches 0, because i have the icon states going by 10s from 0 to 100, like the guy who posted this had.
In response to Dtwon
Dtwon wrote:
Yeah i see what your getting at but in my game the percent is supposed to stay at 10 until it reaches 0, because i have the icon states going by 10s from 0 to 100, like the guy who posted this had.

Then you can do: percent = round(percent + 4, 10), that way when percent is 0, it'll round down (4 rounds to 0) but when percent is 1 it'll round up (5 rounds to 10).