Minor note before we start: I will be using 'usr' in all of these examples because they're all controlled by verbs, but in most 'real' programming cases these actions will be proc controlled, in which case 'src' would be used instead. In review, usr = user (user of the verb), and src = source (source of the proc).
Movement procs: how do they work, and why do I need them?
BYOND has many procs that allow you to control the movement of objects in your world.
Using BYOND's built in movement procs has some advantages and disadvantages compared to coding your own movement system. The built-in procs are easy to use and allow for fast programming -- but they are very specific and limited in what they do.
What you need for this project:
- The BYOND package (Dream Maker/ Dream Seeker)
- A basic world with: turfs, and non-player mobs
First we'll start off with the basic movement procs: step(), and walk().
The step proc is used for single steps in one direction. This can be useful for moving things like blocks, or when you want to have plenty of control over an object's movement.
Here's an example of moving a player one tile north:
step(usr,NORTH)//This calls the step proc for usr with the built-in NORTH direction
The walk proc is good for constant movement. Here's a simple code for moving a person non-stop in the direction they're facing:
walk(usr,usr.dir,0)//This calls the proc for the user of the verb
//with the direction they're facing as the direction, and
//with 0 lag passed into it (increase the lag number to make
// the user walk more slowly).
(To make a verb to stop the constant movement, you'd need to use walk(usr, 0).)
Still simple? Thought so.
Now it's time for variations on the two above procs. We'll start with the "to" procs: step_to(), step_towards, walk_to, and walk_towards.
The step_to() proc makes a mob take one step to another mob, with a minimum distance option (the step will not be taken if the objects are already within the specified distance of each other).
Here's an example on how to make one mob step to another.
for(var/mob/M in oview())//Loops over all the mobs within oview() of you.
step_to(usr,M)//calls the step_to proc and tells it to step_to M
break // halts the for() loop
Eh, not too hard.
The walk_to proc is a great proc; it allows you to make something walk to another thing non-stop... and get this: it follows the other thing unless you tell it to stop! (To tell it to stop, send 0 as the second parameter, as described in "walk", above.)
This example will show you how to make one mob walk to another and follow it.
walk_to_someone(mob/M as mob in world)//allows you to pick who you want to talk to
walk_to(usr,M,1,2)//makes the user of the verb walk to M and stay one tile away, with 2 tick lag delay
Getting fun, isn't it?
Now on to "towards" -- how's it different from "to"? Well, "to" will make the object attempt to avoid obstacles (dense turfs and other objects with density = 1); the "to" commands aren't terribly smart, but they'll at least try to avoid obstacles. The "towards" commands won't even try. When blocked, the moving object will just sit there until its target changes location or the obstacle goes away.
This works a lot like step_to, except it doesn't have the minimum distance option. But it also moves the user in the direction of the other person.
The example from step_to would work here.walk_towards()
walk_towards works a lot like walk_to(), except without the minimum distance option.
This example shows how to make a mob walk to another mob:
walk_towards_someone(mob/M as mob in world)
walk_towards(usr,M,2)//makes the user walk to M with 2 ticks of lag delay
Get me outta here! The "away" procs: step_away(), and walk_away:
This proc is used to make something take one step away from something else.
This example will make the user take one step away from another mob:
step_away_from(mob/M in oview())
step_away(usr,M)//causes the user to step away from M
This proc is used to make something walk away non-stop from something else.
This example will make you "run" away from another mob:
run(mob/M as mob in oview(3))
walk_away(usr,M,5)//Now anytime M gets 5 tiles away from you, you'll run.
Gettin' crazy, the "random" procs, step_rand() and walk_rand():
This proc causes the user to take one step in a random direction.
This example will make the user take a random step:
This proc will make its user walk non-stop in a totally random path.
This example will make your mob go plain crazy:
Well, that's all the movement procs. Go now, have fun, and toy with movement!
Loops: I wanna make that action repeat... I wanna make that action repeat... I wanna... sorry.
Loops are good for all kinds of things, such as making a proc do something over and over to make certain things happen non-stop, and doing something until an action is met.
There are a few procs for this like: while(), do, and for().
This proc is used to loop over lists, plain and simple.
Here is an example of how to loop over all mobs in the world and do something to them:
for(var/mob/M in world)//loops over all mobs in the world
M << "Your name is: [M.name]"//outputs M's name to them.
for() also has a second function.
This example will show you how to make a for() loop keep outputting something until a condition is met. (It works a lot like while()):
for(number=0, number != 10, number++)//This means number is now zero;
//while number isn't 10, add one to number
usr << number//will output 0 1 2 3 4...until 10
Well, the name explains it pretty well. It continues a loop while() its conditions aren't met.
This example will continue adding to a var until that var is 100:
somevar = 0
while(somevar != 100)//while somevar is NOT 100
sleep(10)//sleeps one tick
somevar++//adds one to somevar
usr << "Somevar is now [somevar]"//so you know what it is.
Do is used with while() to repeat a block of code while a specified expression is true. Since the "while" test isn't until the end of the loop, the block will always execute at least once!
Here's an example of how to output "Hello" while a var is equal to one (TRUE):
somevar = 1
sleep(10)//so you don't get spammed
usr << "Hello"
while(somevar)//while somevar is 1 or TRUE
And that concludes this installment of Nadrew's How-To's. Thanks for reading!