ID:263451
 
Code:
mob
proc
attack()
var/mob/M
for(M in get_step(src,src.dir))
if(istype(M,/mob/))
while(M in view(src.range))
while(M in get_step(src,src.dir))
var/damage=round((round(src.attack*2.5)-M.defense)/2)
if(M != get_step(src,src.dir))
break
if(damage<0)
damage=0
src<<"You attack [M] for [damage]."
if(M.hp<=0)
deathCheck(M)
break
sleep(src.delay)
else
world<<"Not a player."


Problem description:
This is causing my game to freeze up and crash. How do I fix this?
Dead_Demon wrote:
This is causing my game to freeze up and crash. How do I fix this?

Simple. You learn to properly code loops. >.> You should go ahead and try by rewriting that small block o' code.
Get rid of the while()'s
You are making things too complicated.

mob
proc
attack()
for(var/mob/M in get_step(src,src.dir))
//attack stuff here


The above code loops through all the mobs directly in front of the caller. You would place your attack code in the for loop. You don't need the if(istype(M,/mob)) since you only process the loop for each /mob in front of the caller. I don't see where you are ever actually removing the calculated damage from M's hp, which means that M's hp will never become <= 0--your if(M.hp <= 0) won't ever execute. Also the method you use for delaying the caller's next attack won't work the way you intend it to.

Basically, it's a mess.

Kaioken has given you some sound advice.
In response to Vermolius
Also note that you'll probably not want to use something as for(var/mob/M in get_step(src,src.dir)) in an attack() verb. That will loop threw ALL the mobs in that turf, and you'll probably only want to attack one at a time. For that you can just use the locate() proc to find a single mob (if any) in the turf.