ID:264351
 
Code:
mob/proc
NPC_Ranged_AI_Attack(mob/T)
var/Proc = "projectile"
walk_away(src,T)
spawn while(T && Target)
if(T.z != z || !(T in orange(src)))
Target = null
NPC_Ranged_AI_Target()
return
sleep(1)
while(T && Target)
if(T.z != z || !(T in orange(src)))
Target = null
NPC_Ranged_AI_Target()
return
sleep(20)
if(hascall(src,Proc))
step_towards(src,T)
call(src,Proc)(/obj/Weapons/Knife)


Problem description:
Unfortunately, the call() right after step_towards isn't executed fast enough, so the NPC has already turned back around and continued walk_away() by the time it does execute. Is there any way to remedy that? Any other critiques would be nice as well.

Here's what it's supposed to do: when called by a proc that handles movement and hostility, this proc takes over and controls the mob's ranged attacks and movement. The NPC will walk_away() from its target, essentially keeping its distance, and every X seconds, whip around and fire off a projectile towards its target, then continue to keep its distance.
you could

spawn(1) call(src,Proc)(/obj/Weapons/Knife)
this would leave the code free to continue moving

OR

split the movment attacking and tracking code
and run them all at the same time