ID:263223
 
Code:
mob/proc/check()
if(src.type == /mob/Player)
var/fHP = (HP / MaxHP) * 100
var/meterHP
if (fHP < 100&&fHP > 89)
meterHP = 90
else if (fHP == 0)
meterHP = 0
else if (fHP == 100)
meterHP = 100
else if (fHP <= 10&&fHP != 0)
meterHP = 110
else if (fHP > 100)
meterHP = 120
else
meterHP = (round(fHP) - 5)
if (((meterHP / 5) % 2) == 1)
meterHP += 1
meterHP = round(meterHP,10)
for(var/obj/HP/P in src.overlays)
P.icon_state = "power_[num2text(meterHP)]"
//check for stat caps
if (src.Strength > 1000000) //max 100 million
src.Strength = 1000000
if (MaxHP > 1000000000) //max 1 billion
MaxHP = 1000000000
if(HP < 0) HP = 0


Problem description:Nothing happens.

overlays isn't a regular list, so unfortunately, you can't loop through it.

What you can do is instead have a seperate list and/or variable to keep track of what you have in the overlays list. Then when you want to make a change, remove the changed item from overlays(using the seperate list/var you have), change the object in that list or var, and then re-add it to the overlays list.

Also, instead of if(src.type == /mob/Player), you might want to use istype(). istype() will still work for subtypes, so src.type could be /mob/Player/Knight and it would still work.
In response to Jon88
Jon88 wrote:
overlays isn't a regular list, so unfortunately, you can't loop through it.

What you can do is instead have a seperate list and/or variable to keep track of what you have in the overlays list. Then when you want to make a change, remove the changed item from overlays(using the seperate list/var you have), change the object in that list or var, and then re-add it to the overlays list.
Can u show to me a example?

Also, instead of if(src.type == /mob/Player), you might want to use istype(). istype() will still work for subtypes, so src.type could be /mob/Player/Knight and it would still work.

"istype(M, /mob/Player)"?

In response to Pharaoh Atem
Pharaoh Atem wrote:
Can u show to me a example?

mob
var/obj/myoverlay/O = new
verb
ChangeOverlay()
src.overlays -= O
O.icon = 'someothericon.dmi'
src.overlays += O

obj
myoverlay
layer = MOB_LAYER+1
icon = 'someicon.dmi'


You could also do something more flexible by using a list. Whenever something was added or removed from regular overlays, you'd add or remove it from the list at the same time. Then you'd have a list that you could loop through, but you'd need to remove and re-add any changed items from overlays so that they would update.

"istype(M, /mob/Player)"?

Yep. That will be more flexible in an if() than checking for only one specific type.
In response to Jon88
mob/proc/check(mob/M)
if(istype(M, /mob/Player))
var/fHP = (HP / MaxHP) * 100
var/meterHP
if (fHP < 100&&fHP > 89)
meterHP = 90
else if (fHP == 0)
meterHP = 0
else if (fHP == 100)
meterHP = 100
else if (fHP <= 10&&fHP != 0)
meterHP = 110
else if (fHP > 100)
meterHP = 120
else
meterHP = (round(fHP) - 5)
if (((meterHP / 5) % 2) == 1)
meterHP += 1
meterHP = round(meterHP,10)
for(var/obj/HP/P in src.overlays)
P.icon_state = "power_[num2text(meterHP)]"
//check for stat caps
if (src.Strength > 1000000) //max 100 million
src.Strength = 1000000
if (MaxHP > 1000000000) //max 1 billion
MaxHP = 1000000000
if(HP < 0) HP = 0

mob
var/obj/myoverlay/O = new
verb
ChangeOverlay()
src.overlays -= O
O.icon = 'someothericon.dmi'//any icon?
src.overlays += O

obj
myoverlay
layer = MOB_LAYER+1
icon = 'someicon.dmi'//any icon?


sorry, but i dont know how to use list =(
In response to Pharaoh Atem
Pharaoh Atem wrote:
sorry, but i dont know how to use list =(

Do you need to use a list? Does each mob only have one HPmeter in their overlays, or does it have lots of them?

If each mob only has the one, you can store it in a variable, sort of like the example.
In response to Jon88
Jon88 wrote:
Pharaoh Atem wrote:
sorry, but i dont know how to use list =(

Do you need to use a list? Does each mob only have one HPmeter in their overlays, or does it have lots of them?

If each mob only has the one, you can store it in a variable, sort of like the example.

each mob has only one.

for(var/obj/HP/P in src.overlays) -> for(var/obj/HP/P in src.defined_var_for_HPMETER) ?
In response to Pharaoh Atem
Pharaoh Atem wrote:
for(var/obj/HP/P in src.overlays) -> for(var/obj/HP/P in src.defined_var_for_HPMETER) ?

That's one way. You can also just use a single variable. If you only have the one, you don't need a for loop.
mob
var
obj/HP/HpMeter = new

//...
src.overlays -= HpMeter
//change HpMeter's variables(ex icon) here
src.overlays += HpMeter
//...
In response to Jon88
and this will be...

for(HpMeter in src.overlays)?
In response to Pharaoh Atem
Pharaoh Atem wrote:
and this will be...

for(HpMeter in src.overlays)?

No. You can't loop through src.overlays. If you have the object you want to deal with in another variable you don't need to. Just subtract that object from src.overlays, change the object, and then add it back in. No loops needed.
In response to Jon88
mob/proc/check()
if(src.type == /mob/Player)
var/fHP = (HP / MaxHP) * 100
var/meterHP
if (fHP < 100&&fHP > 89)
meterHP = 90
else if (fHP == 0)
meterHP = 0
else if (fHP == 100)
meterHP = 100
else if (fHP <= 10&&fHP != 0)
meterHP = 110
else if (fHP > 100)
meterHP = 120
else
meterHP = (round(fHP) - 5)
if (((meterHP / 5) % 2) == 1)
meterHP += 1
meterHP = round(meterHP,10)
src.overlays -= HpMeter
src.overlays += HpMeter
HpMeter.icon_state = "power_[num2text(meterHP)]"
//check for stat caps
if (src.Strength > 1000000) //max 100 million
src.Strength = 1000000
if (MaxHP > 1000000000) //max 1 billion
MaxHP = 1000000000
if(HP < 0) HP = 0
In response to Pharaoh Atem
Close. You need to subtract the object from src.overlays first. Then, change the object's icon. Then you can add the object with the new icon to src.overlays.
In response to Jon88
Jon88 wrote:
Close. You need to subtract the object from src.overlays first. Then, change the object's icon. Then you can add the object with the new icon to src.overlays.
        src.overlays -= HpMeter
HpMeter.icon_state = "power_[num2text(meterHP)]"
src.overlays += HpMeter


finnaly?
In response to Pharaoh Atem
Yep, as long as your mob has a HpMeter variable with a HpMeter in it.