ID:1221427
 
(See the best response by Ter13.)
Code:
mob/enemy
var
spawnloc
damage
mob/target = null
proc/AI()
while(!target)
step_rand(src)
if(get_dist(src,spawnloc >= 5))
step_towards(src,spawnloc)
sleep(10)
while(target)
sleep(5)
src.dir = get_dir(src,target)
step(src,dir)
if(get_dist(src,target) == 1)
if(target.dir == "EAST")
if(src.dir == "EAST")
step(src,NORTHEAST)
src.dir = get_dir(src,target)
if(src.dir == "WEST")
step(src,NORTHWEST)
src.dir = get_dir(src,target)
if(src.dir == "SOUTH")
step(src,NORTHEAST)
src.dir = get_dir(src,target)
if(src.dir == "NORTH")
step(src,SOUTHWEST)
src.dir = get_dir(src,target)
if(get_dist(src,target) == 1)
target.takedamage(damage,src)
else
step_towards(src,target)
target.takedamage(damage,src)
step_away(src,target)


New()
spawnloc = src.loc
AI()


Problem description:
The code says that as long as the mob has no target, just walk within 5 blocks of your spawning point. Unfortunately, no enemies follow this behavior, and I cannot figure out the cause of it. I've tried specifically setting target to null, but that doesn't seem to work, either. Please help with this issue, thank you.


Also, a similar mob in the game is coded with almost the exact same principle, and it works fine:

mob/muggle/var/seenmagic = 0
mob/muggle/Officer
icon = 'npc.dmi'
icon_state = "officer"
var/mob/criminal
proc/AI()
while(!criminal)
step(src,EAST)
sleep(5)
step(src,EAST)
sleep(10)
step(src,WEST)
sleep(5)
step(src,WEST)
sleep(10)
while(criminal)
sleep(5)
step_towards(src,criminal)
if(get_dist(src,criminal) == 1)
criminal.locked = 1
view(src) << "<b>[src]:</b> That's it! I'm taking you to the station!"
criminal = null
New()
AI()
You are never getting out of your !target loop.

proc/AI()
while(!target)
step_rand(src)
if(get_dist(src,spawnloc >= 5))
step_towards(src,spawnloc)
sleep(10)
if(target)
AI()
return


Though, I don't see any point where you are looking for a target.
I actually don't know when to look for a target, but this is not the issue, I'm making an assumption that either the code thinks it has a target, or that neither while(!target) or while(target) are correct, so he just sits there doing nothing.
id:109796

Falacy has a nice tutorial on this.
Ah, it's caused by your New() function, as far as I can tell. Try this:

    New()
. = ..()
spawn()
spawnloc = src.loc
AI()
return .
This didn't seem to work, why would the New() cause this?
From time to time, I've noticed that proc calling in New hang when not spawned() or calling the superclass.

Is your function returning any errors?

Put some debug messages in your while()s to determine if they are ever being called.

Best response
Oh, nevermind, I missed this the first time.

The problem is on this line.

get_dist(src,spawnloc >= 5)


change it to:

get_dist(src,spawnloc)>=5
On a somewhat unrelated note, you should use 'else if' here:
                    if(src.dir == "EAST")
step(src,NORTHEAST)
src.dir = get_dir(src,target)
if(src.dir == "WEST")
step(src,NORTHWEST)
src.dir = get_dir(src,target)
if(src.dir == "SOUTH")
step(src,NORTHEAST)
src.dir = get_dir(src,target)
if(src.dir == "NORTH")
step(src,SOUTHWEST)
src.dir = get_dir(src,target)
Why has no one mentioned that dir is not a string? You simply do this:
dir = NORTH
dir = SOUTH
dir = EAST
dir = WEST
dir = NORTHWEST
dir = NORTHEAST
dir = SOUTHWEST
dir = SOUTHWEST
Thanks a lot, people! Sorry it took so long to reply.