ID:1678961
 
(See the best response by Sir Lazarus.)
Code:
mob
step_size = 4
Player
icon = 'MainCharacter.dmi'
icon_state = "Walk"
Login()
usr.loc=locate(7,24,1)


proc/HpRegen()
while (Hp < MaxHp)
sleep(10)
Hp += rand(3, 7)

verb/Damage(mob/M)
M.Hp -= 10
verb/HealthChech(mob/M)
usr << "You have [Hp] points!"


Problem description:
Yeaah, having issue with such a simple concept. Basically, trying to make a proc that activates the moment your health is lower than your maximum health.

I can't figure what I'm doing wrong. I've even tried using that proc with the additions of arguments ('mob/M'), but nothing.

A damage procedure isn't really made yet since I want to be able to get this part down.For testing purpose, I have a damage verb to try and force the proc into action.
Best response
Try something like this:

/mob/var/hp = 50
/mob/var/maxhp = 50
/mob/var/tmp/regenerating = FALSE

/mob/proc/damage(d)
src.hp = max(0, src.hp - d)

if (!src.regenerating)
src.regenerating = TRUE

spawn
while (src.hp < src.maxhp)
sleep(10)
src.hp = min(src.maxhp, src.hp + rand(3, 7))

src.regenerating = FALSE

/mob/proc/heal(d) src.damage(-d)

/mob/verb/vDamage()
set name = "damage"
src.damage(10)

/mob/verb/vHealthCheck()
set name = "check health"
src << "You have [hp]/[maxhp] hit points."


You should use the damage or heal procs instead of setting hp directly, otherwise health won't be regenerated automatically.
BYOND really doesn't have event listeners without a lot of overhead.

Best practice for something like this would be to use a setter for the Hp variable. Within that variable setter, you would then activate that proc.

mob
proc/SetHealth(change=0)
if(change)
Hp+=change //change
Hp = min(max(0, Hp),MaxHp) //lock to value
DeathCheck()/check if in the range
if(!HpRegen) HpRegen()


Then every time you need to change health, you would use this.

    var/HpRegen //to keep track of active
proc/HpRegen()
if(HpRegen) return //if active, return
HpRegen = 1
while (Hp < MaxHp)
sleep(10)
SetHealth(rand(3, 7))
HpRegen = 0 //cleanup

verb/Damage(mob/M)
M.SetHealth(-10)

verb/HealthCheck(mob/M)
usr << "You have [Hp] points!"
Thank you, Sir Lazarus and Pirion. It's a bit confusing, my I'm understanding a little more. Creating the proc to work only when your health is lower than Max Health isn't enough. I'd have to create another proc to call it.. I get it, though it's a pain. (Correct me if I'm wrong)

As for Pirion, the only thing that confuses me is the use of 'if(!Healing)'.

I'm unsure where that even came from, the 'Healing' part that is.
In response to Gtgoku55
Gtgoku55 wrote:
Thank you, Sir Lazarus and Pirion. It's a bit confusing, my I'm understanding a little more. Creating the proc to work only when your health is lower than Max Health isn't enough. I'd have to create another proc to call it.. I get it, though it's a pain. (Correct me if I'm wrong)

As for Pirion, the only thing that confuses me is the use of 'if(!Healing)'.

I'm unsure where that even came from, the 'Healing' part that is.

My bad - HpRegen and it was redundant anyway.
Thanks! Gonna add this in, play around with it a bit. You were a great help.