ID:139172
 
Code:
mob/Dog
var/Leash
icon = 'Dog.dmi'
New()
..()
Leash = src.loc
Chase()
Bump(atom/movable/A)
..()
if(istype(A, /mob/Player)) step(A,src.dir)
proc
Chase()
for(var/mob/Player/H in range(5,Leash))
H << "Bark"
step_towards(src, H)
if(!H) // This doesn't work
step_towards(src, Leash)
spawn(3) Chase()


Problem description:

The dog is suppose to chase away anyone within view of the location he was created. That works but i can't get him to go back to his original point after he chases people away.
Whatever is under the for() will happen if it the value is true. I suggest you have the variable defined before the loop and check if it does not exist outside:
while(src)   //  infinite loop, replacing your spawn() Chase() statement for the same effect
var/.../H
for(H in X)
This happens when H is found and H will be defined

if(!H)
H is undefined because no one was found during this single loop

sleep(5) // wait ~0.5 seconds before repeating the loop via while
Additional bug: If there are two people in range, the dog steps twice.

locate(/mob/Player) in range(5, Leash)


is your friend.
In response to GhostAnime
Ohh so you're saying that the for statement happens only when the argument is true, so putting if(false) in the statement is pointless because if it were false the statement wouldn't even happen. Right?

Anyway, it works, thanks Ghost and JP. I'm still a little muddy on some points of for() but ill figure it out later.

edit: one last thing, which would be better out of these two?

    proc
Chase()
while(1)
var/mob/Player/intruder
for(var/mob/Player/A in range(5,Leash))
intruder = A
intruder << "Bark"
if(!intruder) intruder = Leash
step_towards(src, intruder)
sleep(3)


    proc
Chase()
var/mob/Player/intruder
while(1)
for(var/mob/Player/A in range(5,Leash))
intruder = A
intruder << "Bark"
if(!intruder) intruder = Leash
step_towards(src, intruder)
intruder = ""
sleep(3)