ID:80233
 
Something I noticed the last time I was scanning through my Blue Book was some funky type tree operators I had never seen used before. After investigating and realizing that part is in the online guide too (whoops, didn't notice!) I decided to enlighten the rest of the world who skipped that part.

When you're writing out some long code for parent and children types you've probably said to yourself 'man there's got to be an easier way to handle this' at least once, this article will show you the way.

There are two operators you can use the . look-up operator and the : look-down operator.

Lets take a look at . first.

obj
myobj
var/my_var
one
my_var = .myobj
two
my_var = .one


This example basically defines a few types and a variable, what the . does here is it'll look up the type tree for matches, if it doesn't find one in the first parent up it'll continue to look until it runs out of places to look. So what we're doing is really setting one's my_var to /obj/myobj and two's my_var to /obj/myobj/one. Still with me? Good.

This can even be done outside of the parent type like so:

obj
otherobj
myobj
var/my_var
one
my_var = .myobj
two
my_var = .otherobj


This example sets my_var of the two object to /obj/otherobj -- saves a bit of typing.

This next example shows how the look-up operator works for variable definition (proc and verb definition too!)

obj
otherobj
var/some_var = 1
myobj
one
.otherobj
var/other_var = 2


This basically creates a new variable for /obj/otherobj under /obj/myobj -- this is very helpful for defining variables close to where they're being used and under the types they're being used on. You'll be able to use the 'other_var' variable from any /obj/otherobj instance.

The look-down operator functions similarly except it works downwards through the type list and not upwards. So anything defined after the object in question will be found by the search.

This makes for an excellent method of organizing your code, you can define things in ways that preserve the code tree while letting you work outside of it.

I imagine there's still plenty to do with these operators that I haven't thought of yet, but this is a start. Good luck on making use of this technique and finding better ways to utilize it to its fullest.

Until next time, keep on programmin'.