ID:1057927
 
Keywords: movement, npc, speed
(See the best response by Kaiochao.)
Code:
mob
Alienbaby
icon = 'beasts.dmi'
icon_state = "alien baby"
Click()
usr << "It seems that they raise alien babies in the future."
New()
walk_rand(src)


I have this file for an npc moving about the screen, but it moves too fast. Is there any way I can slow it down?

Best response
Provide a move delay in the walk_rand().

Or, if you're using pixel movement, give it a lower step size in either walk_rand or the object definition.
This is how I usually handle movement, because I still use byond's built in glide feature. It can be quite good when done properly.

proc/get_speed_delay(n)
if(n != 0)
return (world.icon_size * world.tick_lag) / n
else
return (world.icon_size * world.tick_lag) / 1

proc/get_glide_size(n, dir)
if(dir == NORTH || dir == SOUTH || dir == EAST || dir == WEST)
return n
else
return n + (n / 2)

atom/movable
var/speed = 1
var/tmp/move_time = 0

Move()
if(!src.loc) return ..()

if(world.time < src.move_time) return 0

. = ..()

if(.)
src.move_time = world.time + get_speed_delay(src.speed)
src.glide_size = get_glide_size(src.speed, src.dir)


This rewrites the default Move() for everything, causing anything that moves to require a speed variable which controls how fast it will move. A speed of 1 is as slow as the FPS of the world will let you move with built in gliding. A speed of 10, for example, is 10 times faster than 1.

If you use this, you'll need at least a world.fps of 32 or so. Then, you'd just call walk(ref, direction) or walk_rand(src) and make sure to set the speed, and the rest will be taken care of.