ID:2643354
 
(See the best response by Nadrew.)
Code:
//EnemyMain.dm

mob/Enemy
proc
changeIcon(Icon)
icon_state = Icon
src.density = 0

CombatAI()
if(Hp > 0)
while(src)
for(var/mob/Player/P in oview())
if(get_dist(src,P)<=1)
src.dir = get_dir(src,P)
src.Attack(P)
else
step_to(src,P)
break
sleep(rand(4,8))
else
del(src)



New()
src.CombatAI()
return ..()

mob/Enemy/Weak
icon = 'Enemy.dmi'
icon_state = "Weak"

Hp = 10
maxHp = 10
ExpGive = 25
Def = 3
Str = 5

eRespawn = 100


DeathCheck(mob/Killer)
if(..())
goldDrop = rand(1,25)
OnDeath(ExpGive,goldDrop,Killer)
changeIcon("WeakDead")
sleep(100)
var/mob/Enemy/Weak/W = new(startLoc)
del(src)

mob/Enemy/Elite
icon='Enemy.dmi'
icon_state="Elite"

Hp = 150
maxHp = 150
Def = 15
Str = 25
ExpGive = 100
goldDrop = 0
eRespawn = 100

DeathCheck(mob/Killer)
if(..())
goldDrop = rand(5,100)
OnDeath(ExpGive,goldDrop,Killer)
changeIcon("EliteDead")
sleep(100)
var/mob/Enemy/Elite/E = new(startLoc)
del(src)


//Main.dm

world
turf = /turf/grass



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

var
Hp = 0
maxHp = 0
Damage = 0
Wealth = 0
ExpGive = 0
Exp = 0
ExpNext=0
Level = 1
Str = 1
Def = 1
critRank = 1
goldDrop = 0
eRespawn = 100
dead = 0

logName = ""


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


DeathCheck(mob/Killer)
if(src.Hp < 1)
dead = 1
return 1
else return 0

LevelUp(mob/M)
if(M.Exp >= M.ExpNext)
M.Level += 1
M.Hp += M.Hp/3
M<<"Level Up, Level: [M.Level]"
M.Exp = M.Exp - M.ExpNext
M.Statgain()

Statgain(S,D)
Str += S
Def += D
src << "You have gained [S] Str and [D] Defense!"


TakeDamage(Dmg,mob/Source,mob/DmgTaker)
DmgTaker.Hp -= Dmg
DmgTaker.DeathCheck(Source)

OnDeath(exp,ohms,mob/Killer)
GiveExp(exp,Killer)
DropLoot(ohms)

DropLoot(ohms)
var/obj/Ohms/G = new(loc)
G.value = ohms

GiveExp(exp,mob/Killer)
usr.Exp += exp
LevelUp(Killer)




verb
Attack(mob/M in get_step(src,dir))
set category = "Combat"
Damage = Str - M.Def + rand(critRank*-10,critRank*10)
if(Damage < 0)
Damage = 0
if (M.Hp > 0)
view() << "[usr] hit [M] for [Damage] Damage!"
TakeDamage(Damage,src,M)
else if (M.Hp <= 0)
view() << "[M] Is Already Dead"
DeathCheck(M)


Problem description:
ok, what the hell am I missing here. If you are not dead, you can move, else del src and they still run around dead. But if I put if you are dead move and else del src, del works fucking fine. Please help, this is driving me nuts. I want them to only move if they have Hp > 0. Otherwise just die. Also, while the combat script is running, they wont respawn, but otherwise they will. I think I don't fully understand the while and for loops yet and if thats the problem please tell me.


Best response
Your condition is outside of your loop, the looping code begins when HP is a certain value, after that it's running on its own. You should do something like

while(src && src.HP > 0)
// Do stuff
del(src) // This is reached if both conditions above fail.
Thank you.