ID:1651434
 
(See the best response by Jittai.)
How would I go about creating a loop in which a mob will check if there are any players in the area and do something, or will do something else if there are not any players area?

I've attempted this already, though I failed horribly. Loops are not my strong point.
We're eager to see what you've attempted - as this will allow us to help you out with your issue. :)
Best response
If this is for AI, and checking for active players, you don't need a loop for that.

Just tie into Move() and loop after the fact as long as there's a client mob near by; then obviously deactivate the AI. This will cut down on any activity that you really shouldn't be doing if no one is there to see it.

Also, use while().

There's plenty of tutorials written about this.
In response to Jittai
@Pirion

I deleted the code after it didn't work :c

@ Jittai

In English please xD? I know that I'm to use while (though I think this whole thing would be more effective if somebody could give me an annotated example). I looked into the other tutorials multiple times, though I'm not finding out what I want (this may be a case of not searching hard enough).
What Jittai means is check for inactive monsters whenever someone moves. You can use a for() loop with the range() proc to get an activation distance, and have that activate a while() loop for the AI until there are no players nearby.

If you don't want mobs to be immediately inactive just have the loop run for about 20 seconds or so after a player leaves view.
In response to Reformist
Sounds good. I think I'll be able to do everything except the while() procedure.

EDIT: Just attempted it and I created this:

mob
proc
Animal_Attitude()
while(src)
for(var/mob/M in oview(5))
if(M.client)
step_to(src,M)
if(get_dist(src,M)<=1)
M<<"The animal smells you."
else
continue
sleep(10)



mob
Move()
..()
for(var/mob/Monster/M in oview())
if(range(src,5))
usr<<"You're in range."
M.Animal_Attitude()
else
return


However, it's not working, and at the moment, I'm not too sure why.
That's not what I mentioned. There's no deactivation of the AI.

You'd want to get a var like 'active_AI' for mobs (or in this case 'monster' class mobs) and set it to 1 when a player walks by it. So your while should be while(active_AI) not (src). Then if there's no player mobs near by this sets to 0.

I'd suggest also, for that activation range, to make it M.view+2 (instead of 5) so the AIs activate off-screen a bit. Same for the AI's range().

Obviously you'd want to put some sanity checks like 'if(active_AI) return' in the start of Animal_Attitude() to prevent overlapping loops.

Also, you'd want to prevent AI from activating other AI. It would help if you had a mob for just players, but it's not necessary.

The range() under Move() should e a get_dist, as range is much more costly of a proc to run.

Step_to refers to a ref and target, I think you mixed up the order of src and M.