ID:1860263
 
(See the best response by Lummox JR.)
Code:
            enemy_loop()
while(src) // While we still exist
sleep(5) // Change this depending on how fast you want the enemy to move
for(var/mob/player/M in oview(3)) // If there is a player around 10 spaces...
src.target = M // It sets it to its target
if(get_dist(src,src.target)<3) // If we have a target..
// var/mob/Player/
step_towards(src,src.target)

else // If there isnt a Target....
walk(src,null)


Problem description:
ive tried multiple solutions to this
i just noticed something
all of my games that require a loop crashes
actually it freezes and i dont know another way around it
could you please help me
what is a loop that could be used for enemy
that will not freeze eventually
i ran a check on my games and they time out after around an hour
sometimes less
Make sure enemy_loop() can only start up once.

mob/enemy
var
tmp
thinking

think()
if(thinking) return
thinking = TRUE
spawn()
// start thinking..

// and when we're done..
thinking = FALSE
   New()
. = ..()
spawn()//starts to do the next command
Wander()

is this starting just once per monster
In response to JustinOreo
It looks like it, but that's not the whole picture. If Wander() is called elsewhere and it's not checking for an existing loop that's already running, you'll have two of the same loops running. This will cause your game to crash after some time.

Best response
Looking for a new target every time through the loop is also fairly punishing, especially since this AI runs constantly. (Do yourself a favor and build something in so monsters with no players nearby can be put to sleep for a while, and awoken later.) oview() is not a nice thing to be calling every 5 ticks for every single monster. Instead, try the following:

- If there is no target, look for one within a distance of M tiles. If one is not found, sleep for a medium interval (and randomize it a bit, which should keep all these loops from hitting at once).

- If there is a target, and it's within a certain distance (same z, and get_dist()<=N, where N>M), move towards it and sleep for a short interval.

The reason M<N is that you want to acquire the target within a shorter distance, and lose it after a longer distance. That should help keep the monster locked on.