mob/verb/Attack()
if(src.doing == null)
for(var/mob/Monster/M as mob in get_step(src,src.dir))
if(src.doing == "Fighting")
return
else
src.doing = "Fighting"
src.Combat(M)
return
mob/proc/Combat(mob/M)
if(istype(M,/mob/Monster))
Battle_Start
var/src_agi = roll("1d[src.agi]")
var/mob_agi = roll("1d[M.agi]")
if(src_agi < mob_agi)
src << 'Miss.wav'
else
var/damage = roll(src.dmg_dice) + src.str - M.def
if(damage <= 0)
damage = 0
src << 'Miss.wav'
F_damage(M,damage,"#EF9B84")
else
src << 'Punch.ogg'
F_damage(M,damage,"#EF9B84")
M.hp_min -= damage
if(M.hp_min <= 0)
spawn() src.Death(M)
return
else
sleep(3)
if(mob_agi < src_agi)
src << 'Miss.wav'
else
var/damage = roll(M.dmg_dice) + M.str - src.def
if(damage <= 0)
damage = 0
src << 'Miss.wav'
F_damage(src,damage,"#EF9B84")
else
src << 'Punch.ogg'
F_damage(src,damage,"#EF9B84")
src.hp_min -= damage
if(src.hp_min <= 0)
spawn() M.Death(src)
return
else
sleep(7)
if(src)
if(M)
goto Battle_Start
src.doing = null
M.doing = null
mob/proc/Death(mob/M)
if(istype(M,/mob/Monster))
if(M.hp_min < 0)
src << ">> You've defeated [M.name]."
src.exp += M.exp
src.Update()
if(M.gold)
src << ">> You've found [M.gold] gold coin(s)."
src.gold += M.gold
src << 'Gold.wav'
src.doing = null
new/obj/Blood(M.loc)
del(M)
return
Problem description:
The intended way the code is supposed to work is, the player uses the Attack() command to attack things around them, right now it's just checking for mobs, but later on I intend to allow players to attack other things, like furniture.
Attack() makes sure you aren't already doing something, checks for a monster in front of the player and then calls Combat(), which is supposed to be like a turn based MUD combat system, in which the two sides trade blows until someone runs away or die.
With the code I am showing you right now, having 'return' after calling the Death() proc does not yeild any errors, but simply acts as if the monster never died, it also locks the players stats in fighting mode as if the fight never ended.
If I remove the 'return' after the Death() proc, Death() will be called properly and the monster will die, but then I get an error about the mob no longer being there and being unable to read the vars.
I am sure it has something to do with how I am trying to time all of this out, like something is in the wrong order or something. Can someone PLEASE help me figure this out, I've been trying to fix this for three days.
To
if(M) M.doing = null
Youre getting the error because you're trying to set doing to null when the M has died and no longer exists. The if statement will check if they still exist and if not it won't do anything.
Edit:
?. Would be a tidier alternative to the above as suggested by Spevacus below: