ID:1164145
 
(See the best response by DarkCampainger.)
Code:
proc

Walk()

walk_rand(src,5,8)
..()

Slime

icon = 'Slime.dmi'
icon_state = ""
hp = 10
def = 0
density = 1
New()
.=..()
src.Walk()


Problem description: The slime walks around but only in one direction until it hits something, and then changes direction and continues that way until it hits something again, etc.

What I expect is for the direction of the walk_rand to keep changing values per step. So the slime would move north one step, and randomly move another direction for a step, and then another and another.

Am I doing something wrong? Should I use step_rand instead?

Thanks in advance.

I'm assuming this is defined for a mob

Yes, though. It seems like the better idea to use step_rand()

How are you calling Walk()?
I am still early in my development phase so I have a summon verb that just creates a new /mob/Slime.

When i use step_rand() instead of walk_rand(), the slime is created when I use the summon verb, but it never steps anywhere.

Is there anything else I can do?
Best response
That's how walk_rand() works, I kind of assume it was more designed for town NPCs to mill about, rather than monsters. I think step_rand() may act the same way.

If you want your slime to step more erratically, you'll have to make your own loop:

        proc
Walk()
// We use spawn() to allow Walk() to continue without waiting for the indented code to end (which is an infinite loop)
spawn()
// Loop until this mob is deleted or removed from the map
while(loc)
// Pick a random direction to step in
var/stepDir = pick(NORTH, SOUTH, EAST, WEST)
// Step in our chosen dir
step(src,stepDir,8)
// Wait 5 ticks before doing it all again
sleep(5)


However, if you step truly randomly, keep in mind that the monsters will tend not to move very far or very quickly. It may be worth combining this with step_rand() so they have a chance to move a tile or two before changing direction:
        var
delay = 5
speed = 8
proc
Walk()
// We use spawn() to allow Walk() to continue without waiting for the indented code to end (which is an infinite loop)
spawn()
// Loop until this mob is deleted or removed from the map
while(loc)
if(prob(75))
// Have a 75% chance of using step_rand()
step_rand(src, speed)
else
// Otherwise step in a randomly pick()ed dir
step(src, pick(NORTH, SOUTH, EAST, WEST), speed)

// Wait 'delay' ticks before doing it all again
sleep(delay)


I also moved your speed/delay into variables for easier management.
Hey DC,

Thanks for your reply.

I tried using your second code since I do want a bit of movement from my slime.

The only problem is that the slime does not move at a value of the specified speed all the time.

Sometimes it would move 1 pixel, and then sometimes it would move a very large amount.

The randomized direction works, but the slime is not moving at a constant speed (number of pixels).

What could be causing this?

*EDIT*: I reduced the probability from 75 to 35 and the large jumping has been reduced. It seems my problem has something to do with step_rand. The speed should be 8 pixels for every step, correct?

That isn't what is happening though.