ID:2642567
 
(See the best response by Spevacus.)
Code:
world
turf = /turf/grass



mob
Login()
src.LoadPlayer()
..()

var
Hp = 0

proc
LoadPlayer()
var/mob/Player/P = new()
P.client = src.client
P.Move(locate(/turf/grassStart))

TakeDamage(D,H)
H = H - D
view() << "[H]"

DeathCheck(M)
if(Hp < 1)
del(M)



mob/Player

Hp = 100
var
Str = 5
Damage = 0




verb
say(msg as text in view())
view() <<"[usr] says: [msg]"

OOC(msg as text)
world << "[usr]: [msg]"

Attack(mob/M in oview(1))
Damage = Str
view() << "[usr] hit [M] for [Damage] Damage!"
M.TakeDamage(Damage,M.Hp)
M.DeathCheck(M)


icon = 'player.dmi'


Problem description:

So in my Main.dmi I have a TakeDamage() proc and a DeathCheck() proc.

Take Damage gets called by the player when he attacks the enemy.
then the players attack calls the deathcheck.
My problem is the take damage only happens the first time I attack and I cant seem to get it to happen again.
I feel like I'm only setting the Hp to HP - Str but Im not sure.
Also the mob/Player is in its own PlayerMain.dmi


Best response
You're not actually subtracting anything from the target's Hp variable when you attack them.
Consider your TakeDamage proc's code:
TakeDamage(D,H)
H = H - D
view() << "[H]"

You're setting H to H - D, instead of setting Hp to Hp - D. You're subtracting from H instead of Hp
Revised, this looks like:
TakeDamage(D,H)
Hp = Hp - D
//A shorthand way to write this is...
Hp -= D
view()<<"Current HP is: [Hp]"

Notice how we don't need the "H" argument anymore?

It seems like you got tripped up because the names of your arguments are ambiguous. "H" and "D" aren't very descriptive. I would recommend renaming your arguments to something better understood in the future. In this example, I would have chosen names like "damage" and "currentHealth" or something similar. It sounds like it's not relevant, but your future self will love you for keeping your code readable, and it helps you avoid situations like this.

One more thing to touch on: Your DeathCheck proc doesn't need M passed as an argument if all you're going to do is delete it. Instead, consider passing src as an argument so you could shout out to nearby mobs that (this) mob killed (that) mob.
Here's how that would look:
DeathCheck(mob/Player/Killer)
if(Hp < 1)
view()<<"[Killer.name] just killed [src]!"
del(src)

//Consider handling death in some other way. Deleting the player would disconnect them!

Attack(mob/M in oview(1))
Damage = Str
view() << "[usr] hit [M] for [Damage] Damage!"
M.TakeDamage(Damage,M.Hp)
M.DeathCheck(src)
Thanks that helped me understand it a little better.
I ended up with something very similar and proceeded to make them drop gold but now they drop 3-5 times the gold randomly and i cant figure that out. Progress is steady but slow, yet im super excited to continue learning this script. Byond really clicks with me I feel.