ID:429670
 
(See the best response by Forum_account.)
Code:
mob
var/previouspowerlevel = 0
var/tmp/obj/pl/pl = new // add the tmp var
proc/barupdate()

var/n = round((powerlevel / powerlevel_max) * 100, 10) // calculate the percent

if(n > 100)
n = 101

if(n < 0)
n = 0

pl.icon_state = "[n]" // set the icon state

Stat()
..()

if(previouspowerlevel != powerlevel)
previouspowerlevel = powerlevel
Stat()
..()

if(previouspowerlevel != powerlevel)
previouspowerlevel = powerlevel // set this so it doesn't update every tick

spawn()
barupdate() // update the bar
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


obj
pl
icon='Heallthbar(1).dmi'
layer = 100
pixel_y = -32


Problem description:
The Health Bar don't go up or down it just stay's Full, can someone tell me what i did wrong ?

You can't have 2 Stat()
If the health bar is an overlay, you can't just change the icon_state. You have to remove it, change the icon_state, then add it back.

The two Stat() procs will work because ..() is called in the second one. Calling ..() will call the parent proc or a "sibling" definition of the proc. It's not necessary here, but it'd work.
how do i add it and remove it everytime ?
In response to Forum_account
Forum_account wrote:
The two Stat() procs will work because ..() is called in the second one. Calling ..() will call the parent proc or a "sibling" definition of the proc. It's not necessary here, but it'd work.

Woops, i didn't see the ..() Sorry.
In response to Kaseem
Best response
Just remove it and add it back in the barupdate() proc:

// instead of just doing this:
pl.icon_state = "[n]"

// do this:
overlays -= pl
pl.icon_state = "[n]"
overlays += pl

Or you can use the Overlays library for this:

mob
var
Overlay/health_bar

Login()
..()
health_bar = overlay("")
barupdate()

proc
barupdate()
var/n = round((powerlevel / powerlevel_max) * 100, 10)
if(n > 100) n = 101
if(n < 0) n = 0

health_bar.IconState("[n]")
thanks