Nadrew's How To: Looking up and down the type tree

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'.

Posted by Nadrew on Monday, August 31, 2009 11:58AM - 0 comments / Members say: yea +4, nay -1

« Nadrew's How To: Dynamically named verbs · Game in a Day 2009 »

Login to post a comment.

 

 

Programming Help

Get Started - A quick overview of the tools available for learning to program in BYOND.

DM Guide - This is the first place you should look if you're new to BYOND's language DM. Some people call this the "blue book".

ZBT parts 1, 2, & 3 - Zilal's excellent tutorial series, teaching you the basics of how to get started writing games in BYOND.

Your First World and Step BYOND - Sample games that you can study to help you learn.

DM Reference - Handy for novice users and advanced programmers alike, this documents everything you need to know about the DM language.

Skin Reference - If you want to give your game a custom interface, this tells you what you can do with "skins" and how to work with them.

BYOND Resources - Demos and libraries you can download to help you with your creation.

Publishing Games

Publishing Games - A guide for putting your creations on the BYOND hub to share with others.

Recording Videos - If you want to find out how to make a video of your game and put it on YouTube or another site, this can get you started.

Keywords