ID:273384
 
Hi there im currently making a bleach game and i would like it when you attack a person or the attack u that ur health meter appears and everyone on the screen can see it over ur head. and after about 2 secs its goes away.
obj
health
icon = 'meter_01.dmi'
layer = MOB_LAYER+100
icon_state = "100"
New()
var/mob/O = src.Gowner
var/percent=round(O.health/O.maxhealth*100,10)
if(percent>100) percent=100
if(percent<0) percent=0
for(var/obj/health/o)
o.icon_state=num2text(percent)
spawn(20)
del(src)

thats how i tried but kept getting errors. The bar appears and i can see it on te npc but doesnt go away and it has that error.
runtime error: Cannot read null.health
proc name: New (/obj/health/New)
usr: Mike (/mob)
src: the health (/obj/health)
call stack:
the health (/obj/health): New(the grass (42,89,1) (/turf/Turf/grass))
Mike (/mob): Attack()


The src.Gowner variable is null. You'll need to pass an argument to the New() proc.
In response to Garthor
what do you mean youll need to pass a new argument?
In response to Vai God69
You need to pass the owner.

var/obj/x = new /obj/specialobj(src) // Pass "src" to New()

....

obj/specialobj
New(mob/M) // So we've passed a mob, let's call him M
..()
owner=M // Do something with M
In response to Emasym
okz ive done this .
var/x = new/obj/health(M)
x:Gowner = M


obj
health
icon = 'meter_01.dmi'
layer = MOB_LAYER+100
icon_state = "100"
New(mob/M)
..()
src.Gowner = M
var/percent=round(M.health/M.maxhealth*100,10)
for(var/obj/health/o)
if(M==src.Gowner)
o.icon_state=num2text(percent)
spawn(20)
del(src)

but nothing hapens. theres no errors and theres no icon.
In response to Vai God69
That would be because it's in your contents.

Also, I just noticed that really incredibly ill-conceived for() loop there. What are you even trying to do, there? You can really just delete it entirely, and use src instead of o for the icon_state=blah line.
In response to Garthor
okz here ive made some progress.. The health bar pops up and has the correct icon state and stuff but it wont delete after the set amount of time.. can u help.
var/x = new/obj/health(M)
x:Gowner = M
M.overlays += x
spawn(20)
for(var/obj/health/o)
if(o.Gowner ==M)
del (o)

obj
health
icon = 'meter_01.dmi'
layer = MOB_LAYER+100
icon_state = "100"
pixel_y = 32
New(mob/M)
..()
src.Gowner = M
var/percent=round(M.health/M.maxhealth*100,10)
if(M==src.Gowner)
src.icon_state=num2text(percent)
spawn(20)
del src
In response to Vai God69
Why would you take the loop I told you to completely nix and move it elsewhere? Delete it.

Additionally, you don't need if(M == src.Gowner) because you JUST ASSIGNED that variable.

You don't need to assign x:Gowner either because, again, you JUST DID THAT inside the health's New() proc.

Also, your syntax for spawn(), while it technically works, is very very poor. You should be indenting anything that goes within the spawn() block to be within the spawn() block, like so:

spawn()
ooga
booga
otherstuff



Also, the overlay doesn't disappear because you never remove anything from overlays. Whenever you add something to overlays, it instead is adding an object which has the same appearance as that something, NOT the something itself. Changing the something afterward will not affect the overlay.
In response to Garthor
OKz now this what ive done..please if someone knows how to do it could you show me.it does the same thing even with ur edit garthor.
var/x = new/obj/health(M)
M.overlays += x

obj
health
icon = 'meter_01.dmi'
layer = MOB_LAYER+100
icon_state = "100"
pixel_y = 32
New(mob/M)
..()
spawn(20)
del src
src.Gowner = M
var/percent=round(M.health/M.maxhealth*100,10)
src.icon_state=num2text(percent)
In response to Vai God69
Again, if you are not REMOVING something from overlays, then nothing will be removed from overlays. Deleting the object DOES NOT WORK, as the object itself is not in the overlays list, just its appearance data.
In response to Garthor
ok but then could u help me code something..where a health bar with the correct icon state like 100 for 100% health would be show when the person is attacked and then it would go away after a lil bit.
In response to Vai God69
That is what I am doing. I am telling you that deleting the object is not accomplishing anything and you instead need to remove it from overlays when you don't want it any more.
In response to Garthor
okz thanks very much i figured it out..thanks for all the help Garthor