ID:159935
 
How do I make a working one?

obj/healthbar
icon='hp.dmi'
icon_state="10"
layer=FLY_LAYER+10111
mob/New()
..()
if(src.client==null)
var/obj/O=new /obj/healthbar/
src.overlays+=O
O.owner=src

mob/proc/Updateh()
for(var/obj/healthbar/H in world)
var/mob/M=H.owner
H.icon_state="[round(M.health/M.Mhealth)*10]"


What am I doing wrong? I think it's because either:
1. The healthbar is in it's owners overlays and not in the world.

2. The overlays only contains the healthbar's icon.
obj/healthbar
icon='hp.dmi'
icon_state="10"
layer=FLY_LAYER+10111
pixel_y=32 // Forgot to make it GO over the head. :P
mob/New()
..()
if(src.client==null)
var/obj/O=new /obj/healthbar/
src.overlays+=O
O.owner=src

mob/proc/Updateh()
for(var/obj/healthbar/H in world)
var/mob/M=H.owner
H.icon_state="[round(M.health/M.Mhealth)*10]"


There you go. :|
Not sure if that's what you wanted, because I didn't understand exactly what you meant.
In response to Euphemism
Euphemism wrote:
> obj/healthbar
> icon='hp.dmi'
> icon_state="10"
> layer=FLY_LAYER+10111
> pixel_y=32 // Forgot to make it GO over the head. :P
> mob/New()
> ..()
> if(src.client==null)
> var/obj/O=new /obj/healthbar/
> src.overlays+=O
> O.owner=src
>
> mob/proc/Updateh()
> for(var/obj/healthbar/H in world)
> var/mob/M=H.owner
> H.icon_state="[round(M.health/M.Mhealth)*10]"
>

There you go. :|
Not sure if that's what you meant, because I didn't understand exactly what you meant.

Well, thanks for teaching me something new. Didn't know you could do that. :)

But I was talking about how the health bar doesn't update correctly.
In response to Aixelsyd
Becuase of your formula which is wrong to get a percent.

obj/healthbar
icon='hp.dmi'
icon_state="10"
layer=FLY_LAYER+10111
pixel_y=32 // Forgot to make it GO over the head. :P
mob/New()
..()
if(src.client==null)
var/obj/O=new /obj/healthbar/
src.overlays+=O
src.Updateh()

mob/proc/Updateh()
for(var/obj/healthbar/H in src.overlays)
H.icon_state="[round(src.health/src.Mhealth)*100]"
spawn(10)Updateh()


Also, i edited your code alitle bit, i don't think using for() to loop in the world would work right at all and may cause lag.
In response to Danny Kenobi
Also, having such a high layer hp bar will clash with other icons. I.E. walking behind trees/bushes while the health bar remain above.

If this is intentional disregard this comment...
In response to Danny Kenobi
Wouldn't it be better to have the proc called every time the mob takes damage rather than looping it every second constantly?
it works thanks Danny Kenobi but im trying to make an exp bar thats only for my player.

know any ways that i can edit it so only the usr has the exp bar and the screen_loc is not under the player please help.

its appriciated ;)
In response to Danny Kenobi
Danny Kenobi wrote:
Becuase of your formula which is wrong to get a percent.

obj/healthbar
> icon='hp.dmi'
> icon_state="10"
> layer=FLY_LAYER+10111
> pixel_y=32 // Forgot to make it GO over the head. :P
> mob/New()
> ..()
> if(src.client==null)
> var/obj/O=new /obj/healthbar/
> src.overlays+=O
> src.Updateh()
>
> mob/proc/Updateh()
> for(var/obj/healthbar/H in src.overlays)
> H.icon_state="[round(src.health/src.Mhealth)*100]"
> spawn(10)Updateh()

Also, i edited your code alitle bit, i don't think using for() to loop in the world would work right at all and may cause lag.

Technically, your approach would still cause unneeded lag.
Instead of looping the Updateh(), just only call Updateh() when there could be a possible change in health, such as Damage, Heal, etc.

So, stick with
obj/healthbar
icon='hp.dmi'
icon_state="10"
layer=FLY_LAYER+10111
pixel_y=32

mob/New()
..()
if(src.client==null)
var/obj/O=new /obj/healthbar/
src.overlays+=O
src.Updateh()

mob/proc/Updateh()
for(var/obj/healthbar/H in src.overlays)
H.icon_state="[round(src.health/src.Mhealth)*100]"


but only call Updateh() when the target mob's health could potentially change. (Ending of an Attack or Damaged proc, after a Healing spell or Poison spell, or after something like Eating an apple or health potion.)
mm yes thanks but i only want it for my usr chacter ONLY for him.

i only want to see 1 exp bar on screen which shud be mine
I believe what you're asking for is a hud only visible to you. .. Right? So I found a library created by a quite well-known member.

Hud Groups by Forum_account.

When you get into the library, open the Health-bar-demo folder and check off demo.dm and demo.dmm. A health bar appears in the bottom left of the screen.

In the code, you can adjust it and change it to an experience bar.
Make sure you read hud-groups.dm and the demo.dm you checked off. :)

In case you don't want to look into the library, you may want to look into
-Hud / Screen Objects
-Screen var (client)
-Screen loc
thanks but i dont understand that library im reading it i just dont understand
i dont like to just copy i like to know what im doing and i dont with what he has there ;(
1. Forum_account's Overlays library would be more suitable for a health bar overlay only visible to the player.
mob
var tmp/Overlay/health_bar

Login()
..()
// Makes an overlay on this mob.
health_bar = overlay('health bar.dmi')

// Makes the overlay visible only to this mob.
health_bar.ShowTo(src)

proc
// The health bar needs to be changed
// when your health has changed.
// Call this when you think it is necessary.
update_health_bar()
// Get a percentage, round it,
// and update the overlay.

// You're probably not going to have a health bar
// with 100 icon states for each percentage of health.
// This assumes your health bar icon has 10 states:
// "0" (empty), "10", "20", "30", "40",
// "50", "60", "70", "80", "90", "" (full)

var health_bar_detail = 10 // (100% / 10 states) = increments of 10
var state = "[round(health / max_health * 100, health_bar_detail)]"

// This will update the icon state
// of the health bar overlay
health_bar.IconState(state)


2. You (pretty much) can't loop through the overlays list. You're going to need a secondary list if you want to do something like that.

3. An overlay has to be removed before being modified, and then re-added. Failure to do this will cause the overlay to be stuck with no way to remove it other than by clearing the overlays list.

4. FLOAT_LAYER is actually -1.
Any negative number will have the FLOAT_LAYER functionality, and the greater the number, the higher the layer (an overlay with layer = -1 will show up above something with layer = -2).
The FLOAT_LAYER functionality is that the layers only interact with other overlays on the same object.
Objects with a FLOAT_LAYER that aren't on an object will, I think, show up how you would expect (below most everything else).
(side note: layers can only go from I think -100 to 100, but they can also be accurate up to something like 1e-8 precision)