Geas MUD, enter the Aventure!
Geas Home | Play the Game

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

3.4.2 Abusing defines

A common mistake is to put HUGE arrays and mappings in a define. It's very tempting really, assume for example that you have a mapping that contains the definitions of guild ranks, descriptions, various skill limits, benefit adjustors etc in one big mapping with the rank as index. Very often you'd then need to index that mapping to look up things. Probably it'll be done dozens of times in the central guild object. You'd have something like this:

 
// Top of file

#define GUILD_MAP ([ "new": ({ "beginner", "Utter newbie", 3, 2343, ... }), \
                     "older": ({ ....                                        \
                     ... /* Perhaps another 10-20 lines or more */        \
                   ])

// code, example of use
        write("Your rank is: " + GUILD_MAP[rank][1] + "\n");
// more code...

However... just pause for a second and consider what the #define statement really does... well, it substitutes whatever you had as a pattern for the #define body. So, in every instance where you had written GUILD_MAP the entire mapping would be copied in. And every time it was put in, the gamedriver would have to interpret, store and index the mapping again. It doesn't take a genius level of intelligence to realize that this is a horrible waste of both memory and time.

So... instead of doing it this way you store the mapping in a global variable. Then you use that variable as you use the define. I.e.

 
// Top of file

mapping GuildMap;

create_object()
{
    // code

    GuildMap =  ([ "new": ({ "beginner", "Utter newbie", 3, 2343, ... }), \
                   "older": ({ ....                                        \
                   ... /* Perhaps another 10-20 lines or more */        \
                 ]);
}

// code, example of use
        write("Your rank is: " + GuildMap[rank][1] + "\n");
// more code...



This document was generated by Ronny Wikh on July, 8 2003 using texi2html