ID:142729
 
Code:
mob
proc
ExcessCheck()
if (usr.HP > usr.maxHP)
if (usr.mana > usr.maxmana)
usr.mana = usr.maxmana
usr.HP = usr.maxHP
else
usr.HP = usr.maxHP
if (usr.HP <= usr.maxHP)
if (usr.mana > usr.maxmana)
usr.mana = usr.maxmana

obj

item
Food
icon = 'pub.dmi'
icon_state = "food"
verb
Eat()
usr.HP += 5
usr.mana += 3
alert("your HP has been healed by 5 points and your mana by 3 points.")
del(src)
sleep(1)
usr.ExcessCheck()


Problem description:

That's part of the code of a game I'm helping to code. ExcessCheck proc tries to reset the HP and Mana if they're higher than the limit after using a heal item. But it still goes higher than the max amount it should, and I have no idea of how to fix it.

First of all, don't use usr in procs. Change it all to src.

Second, the whole proc is just buggered up. All you need is:

HP = min(HP, maxHP)
mana = min(mana, maxmana)


Third, the proc is never called because the line del(src) stops the verb.

Fourth, you should just be using a setter function for variables that you want to be capped, and then use that all the time. IE:

mob/proc/setHP(var/newHP)
//0 < HP < maxHP
HP = max(0, min(newHP, maxHP))


That way, you can never forget to cap the variable.
In response to Garthor
Garthor wrote:
mob/proc/setHP(var/newHP)
> //0 < HP < maxHP
> HP = max(0, min(newHP, maxHP))


Inconscistent Identation error when I try to compile. And how do I call that proc when I need it?
Anyway, thanks for the help.