ID:264661
 
Code:
mob/proc/Follow()
for(var/mob/M in world)
if(M.name==src.name)
walk_towards(src,M,0)
sleep(1);Follow()


Problem description: This code send me errors:
runtime error: Maximum recursion level reached (perhaps there is an infinite loop)
To avoid this safety check, set world.loop_checks=0.
proc name: Follow (/mob/proc/Follow)
usr: CarCar (/mob)
src: CarCar (/mob)
call stack:
CarCar (/mob): Follow()
CarCar (/mob): Follow()
CarCar (/mob): Follow()
CarCar (/mob): Follow()
CarCar (/mob): Follow()
CarCar (/mob): Follow()
CarCar (/mob): Follow()
CarCar (/mob): Follow()
CarCar (/mob): Follow()
CarCar (/mob): Follow()
...
CarCar (/mob): Follow()
CarCar (/mob): Follow()
CarCar (/mob): Follow()
CarCar (/mob): Follow()
CarCar (/mob): Follow()
CarCar (/mob): Follow()
CarCar (/mob): Follow()
CarCar (/mob): Follow()
CarCar (/mob): Fusion Dance(CarCar (/mob))



That is because you are calling the proc recursively. The call stack shows you the issue: each instance of Follow() is calling Follow(), which calls another instance of Follow(), and so on. Each one waits for the next to finish - which they never do - until the call stack is full (read: you're out of memory) and it crashes.

Of course, you don't need any of this at all as a single call to walk_towards will continue forever. You also don't need to loop through every mob in the world as presumably you have an actual instance of the mob whenever you first call Follow().
In response to Garthor
Ok, now i have other problem. It works. but when the M Moves the walk_towards stop working.
Yes, the walk() procs will stop when something else causes the mob to move. If you don't want that to happen, then don't allow them to move, by overriding client/Move().
In response to Garthor
Garthor wrote:
Yes, the walk() procs will stop when something else causes the mob to move.

Not quite accurate. It's simply that, by default, player-initiated movements will cause walking on the mob to cease because client/Move() calls walk(src.mob,0). So movements invoked differently on the mob won't stop walking.
In response to Kaioken
That's good, now I feel less guilty for not addressing the issue of something else (like, say, getting pushed) interrupting the movement.

Of course, I would never, ever use any walk() procs myself, so I'm not exactly well-versed in them.
In response to Garthor
Thanks.