ID:2026330
 
(See the best response by Ter13.)
Code:
while (1)
walk_rand(src,10)
sleep(10)

Problem description: I use the above code to make an npc move randomly, which works fine except it also moves diagonnaly (and its movement state has #4 Dirs wtf?) while I'd want to make it move only up/down/left/right. How can I fix this?

Best response
Now, walk_rand() keeps walking until it's stopped. since you are doing all the looping behavior yourself, you could just get away with:

var
list/cardinal_dirs = list(NORTH,SOUTH,EAST,WEST)

while(loc)
step(src,cardinal_dirs[rand(1,4)])
sleep(10)


But if you actually wanted to duplicate walking behavior yourself, which will keep walking randomly without you having to tell it to loop:

var
list/cardinal_dirs = list(NORTH,SOUTH,EAST,WEST)
mover/rand_walk/rand_walk = new()

atom
movable
var/tmp
mover/mover
mover_start

mover
proc
Start(mob/m,delay)
rand_walk
Start(mob/m,delay)
set waitfor = 0
if(m.mover!=src)
var/time = world.time
m.mover = src
m.mover_start = time
while(m.mover==src&&m.mover_start==time)
step(m,cardinal_dirs[rand(1,4)])
sleep(delay)


Now just call rand_walk.Start() to start moving a mob randomly.

rand_walk.Start(src,10)


And just set mover to null to stop random walking.

src.mover = null