ID:144498
 
Code:
    //this comes after my proc for a new game       
usr.overlays+=new/obj/meter/healthbar
usr.overlays+=new/obj/meter/manabar
usr.updatebar()
obj

var/width
meter
healthbar
name=""
icon='bars.dmi'
icon_state="0"
pixel_y=27
layer=MOB_LAYER+10
width=28
var/num=0
manabar
name=""
icon='bars2.dmi'
icon_state="0"
pixel_y=22
layer=MOB_LAYER+10
width=28
var/num=0
mob
proc/updatebar()
world<<"TEST"
for(var/obj/meter/healthbar/o in src.overlays)
world<<"TEST 2"
o.num=(src.HP/src.MAXHP)*o.width
o.icon_state = "[round(o.num)]"
src.updatebar()
if(o.num < 0)
o.num = 0
else if(o.num > o.width)
o.num = o.width


Problem description:
I'm trying to make it so when the player logs in they get a health and magic bar.......and this bar goes up or down depending on their health or magic left. I'm horrible with overlays and I can't figure out what I need to do above. The above gets past the first "TEST" but not the 2nd one. I've looked and looked at tutorials, but they dont help much. So far it only displays the empty health/magic bar. I want them both to display over the usr's head. I'm asumming I messed up somewhere in the defining....

The problem is that the overlays list is special, and you can't directly access objects you put into them. You'll have to create a separate list to hold your overlay objects. If you don't want to store a separate list, and the health and mana bar are the only overlays your players will ever use, then you can use overlays.Cut() to get rid of all the overlay images, and keep re-adding the overlays when your health or mana changes.

Alternatively, you can use the better and more flexible alternative, image objects. That way, you can set the image object's location onto your mob, and just update the image's icon_state whenever your health or mana bar needs to be updated. You also have the flexibility of choosing who can look at it.
In response to Unknown Person
I am not sure how to do lists....could you show me how? I need to learn them.
Also the 2nd way your talking about...I would just change them to objects, set their position to the player's position when created, and then have the object follow the player? I did that with my GUI, so I think i'll be able to do it that way....but I do want everyone to be able to see the HP and MP...how would I do this?
my GUI code is below

obj
hud
pickupbutton
icon='misc.dmi'
icon_state="pickup"
name="Pick Up"
New(client/C)
screen_loc="2,1"
C.screen+=src
layer=MOB_LAYER + 1
attackbutton
icon='misc.dmi'
icon_state="attack"
name="Attack"
New(client/C)
screen_loc="1,1"
C.screen+=src
layer=MOB_LAYER + 1
Click()
usr.attack()
client/New()
..()
new/obj/hud/attackbutton(src)
new/obj/hud/pickupbutton(src)

I only put the first 2 buttons because there was no point in putting all the others, they are basicly the same
Then to make the HP and MP bars follow I'd need to do the below right?
client
North()
..()
for(var/obj/healthbar/O in world)
if(O.owner==usr)
O.loc=locate(usr.x,usr.y,usr.z)
South()
..()
for(var/obj/healthbar/O in world)
if(O.owner==usr)
O.loc=locate(usr.x,usr.y,usr.z)
East()
..()
for(var/obj/healthbar/O in world)
if(O.owner==usr)
O.loc=locate(usr.x,usr.y,usr.z)
West()
..()
for(var/obj/healthbar/O in world)
if(O.owner==usr)
O.loc=locate(usr.x,usr.y,usr.z)

what do I need to change to make it work with the healthbars? Also I noticed a problem with my name system, if the usr double clicks to move, the name does not follow the usr exactly. Will I have this problem with the hpbars also? How do I fix it?
In response to FriesOfDoom
bump
In response to FriesOfDoom
Trust me, go with an image object on this one as earlier suggested. But don't use them as overlays, just attach them to the usr.

I haven't looked at all of your code yet, but I believe the easiest way to do this would be to change healthbar and manabar from obj to image

Then, add vars to your mob that for the healthbar and manabar, and where you have usr.overlays+= stuff, instead put
healthbar.loc=usr
usr<<healthbar
manabar.loc=usr
usr<<manabar


This way, the images follow the usr automatically, so you can delete all that code that attempts to force the bars to follow around.

That may not be all the code you need, but should put you on the right track.
In response to Neo Skye
That is correct ASSUMING that he wants the HP/MP bars ONLY shown to certain players, which he does NOT want because in the post previous to you, he stated "but I do want everyone to be able to see the HP and MP".

To make everyone see it, than it MUST be overlayed (or have a modified move() which adds the "M" mobs bars and locate to them if you're using /image without overlaying.. but that's a bit more troublesome).

Simplest way to do something like what he wants is to make it overlayed and using pixel_y to "bump up" the spot. And to make it even more better, have a general take-stat procedure which updates the Bars whenever the bar stats are added/subtracted, reducing lag compared to making mob/Stat() updating it or call it unneccessarily when making the mob move.
obj/HUD/MP_Bar
layer=MOB_LAYER+1 //will appear over a mob if there's one in its' location, than disappearing under neath it
pixel_y=32 //when overlayed, it'll appear just north of the user by one square.


And fries, you could've avoided all those client/North() and etc by just modifying the client/Mov() ... those procs, however, are called when the user presses one of those DIRERCTIONAL KEYS... maybe what you should do instead is just modify mob/Move()

- GhostAnime
In response to GhostAnime
Sorry, I misread his second post. I thought he had said that he DID only want to show the graphic only to the player. Sorry bout that.
In response to Neo Skye
It's no problem, I thought that too at first >_>

- GhostAnime
In response to GhostAnime
GhostAnime wrote:
And fries, you could've avoided all those client/North() and etc by just modifying the client/Mov() ... those procs, however, are called when the user presses one of those DIRERCTIONAL KEYS... maybe what you should do instead is just modify mob/Move()

- GhostAnime

Lol, I realize now I was scarce on saying what I was trying to say, silly me :P. The above code is from my GUI and code that diplays the usr's name(Changed the name so its an overlay instead to fix glitch of moving with mouse). I was just asking in what ways should I change it to make it work with the HP and MP bars. But I think i figured the part out of making it into an overlay, I just can't figure a way or updating this overlay with a proc. I can't visualize it.

Sorry for the confusion, thx for the help so far :D

this is all I could get so far....all it ends up doing is deleting the healthbar....
mob
proc/updatebar(obj/meter/healthbar/o in src.overlays )
world<<"TEST"
src.overlays-=/obj/meter/healthbar
o.num=(src.HP/src.MAXHP)*o.width
o.icon_state = "[round(o.num)]"
if(o.num < 0)
o.num = 0
else if(o.num > o.width)
o.num = o.width
src.overlays+=o
In response to FriesOfDoom
bump
In response to GhostAnime
GhostAnime wrote:
That is correct ASSUMING that he wants the HP/MP bars ONLY shown to certain players, which he does NOT want because in the post previous to you, he stated "but I do want everyone to be able to see the HP and MP".

No, you can display the /image object to anyone you want, including 'world', which essentially displays it to everyone. You just have to make sure that you display it to people who log on after the /image object was created and initially displayed.
In response to Unknown Person
Hm, didn't know that. Guess you learn something new every day o.o

- GhostAnime
In response to FriesOfDoom
No, that's not the way I was mentioning. You actually create an instance of a /image path type, which is an object that is purely visual. You can stick image objects anywhere you want. An image object's 'loc' is where it will be placed on, and it will move with whatever it is attached to (if it's a movable atom like a mob).

In order to do this, you can define your hpbar and mpbar as a variable each to keep track of the image objects, and make your image object update accordingly in an Update proc by just modifying its icon_state. After, you can display the image objects to the world after they have updated. Make sure you call UpdateBars() whenever your health or mana changes.

mob
var
image/hpbar
image/mpbar

Login()
..()
UpdateBars()

proc
UpdateBars()
// if hpbar and mpbar are null, then create them
if(!hpbar) hpbar = image('hpbar.dmi', src)
if(!mpbar) mpbar = image('mpbar.dmi', src)
// image() takes three arguments, which is the
// image's icon, its location (where it's being stuck
// onto), and it's initial icon_state. We don't set
// the icon_state because it will get updated in the
// following code.

var/hpstate = round((src.HP / src.MAXHP) * 28)
var/mpstate = round((src.MP / src.MAXMP) * 28)
if(hpbar.icon_state != hpstate)
// only change the state and update when it's
// necessary
hpbar.icon_state = hpstate
world << hpbar // display the bar to everyone
if(mpbar.icon_state != mpstate)
mpbar.icon_state = mpstate
world << mpbar