ID:267397
 
How can i get it so the HP can't go over the Max HP



Heres muh code




mob
Stat()
statpanel("[src]'s Statistics")
stat(src)
stat("")
stat("[src.name] [title]", "")

stat("")
stat("HP", "[num2text(HP,100)]")
stat("Maximum HP:", "[num2text(Maximum_HP,100)]")




(dont worry about the identation.... it came out weird....


DBZAS_Vegeto ([email protected])


This is taken from the Stats.dm file within my Login-Tutorial found here.
mob/Stat()
statpanel("Stats")
stat("Health: ","[HP]/[MaxHP]")
StatCheck()

mob/var
HP = 100
MaxHP = 100

mob/proc/StatCheck()
if(src.HP > src.MaxHP) src.HP = src.MaxHP
if(src.HP < 0) src.HP = 0

--Lee
In response to Mellifluous
Thanks it wokrs
In response to DBZAS_Vegeto
Yep, I know it does :)

I specifically made it to work with my game... Then I put it in with my Login-Tutorial for others to use, considering most games use HP, Mana and whatever else they intend to use with stats while intending them not to go below a certain number, 0 by default. As well as making sure the current number does not breach the maximum set number.

In shorter, yet more understanding terms, this little procedure will work with any type of game and will ensure that certain stats will not do what the user intends them to do. For example; It will not allow the HP to go above its maximum defined value... If it breaches that value, it is set at the maximum.

Same goes for any other stat such as Mana.

--Lee
Lummox JR wrote an excellent BYONDscape article on using datums to contain stats like HP, you might want to look it up.

For the most part, all you really need is something like this:

<code>mob var/hp = 20 var/max_hp = 20 proc/IncreaseHP(amount) hp = min(hp + amount, max_hp) proc/DecreaseHP(amount) hp = max(hp - amount, 0)</code>

Look up the min() and max() procs in the reference for more details on how this works.
In response to Foomer
Wow! I had no idea that those procs existed! (Probably because I only search through reference to check something I already have an idea of)... And to think, all this time I have been using lousy 'IF' checks...

-=Ken=-