ID:141864
 
Code:
if(M.hp > 0) // If the monster isn't dead
M.npc_atk = 1
else
var/Mloc = M.loc
var/Mtype = M.type
M.npc_atk = 0
del(M)
spawn(40) new Mtype (Mloc)
src.exp += M.exp_given
src << "You gain experience!"
levelup(src)


Problem description:

My code compiles fine, yet when I kill a monster in the game, the experience isn't added, and it gives me a null var error. The var is properly defined in the monster, so I don't know what the problem is. Can anyone help me?

mob
proc
Death(mob/M)
if(M.hp > 0) // If the monster isn't dead
M.npc_atk = 1
else
//if i were you i would use Del() in the monster path
//by the way you obiously will get an undefined var as you deleted M and called it after deletedo.O
src.exp += M.exp_given
src << "You gain experience!"
levelup(src) // i would use src.levelup() there instead of that
del(M)
Monster
Saibaman
Del()
var/turf/oldloc = loc
..()
sleep(40)
var/mob/Monster/Saibaman/S =new()
S.loc = oldloc


In response to Danny Kenobi
Oh.

...

Thanks.

Never noticed that.
In response to Armiris
By the way, you're deleting M before you're accessing its variables, causing you to attempt to access variables of something that no longer exists.
In response to Danny Kenobi
Calling Del() directly isn't robust; del() is better.
In response to Kaiochao
I knew that. That's what I didn't notice earlier.