ID:36822
 

First off, you're probably asking, "What is a preprocessor directive?" In short, when you compile your code, they're handled before anything else, and they can actually "change" your code so that you can simulate the effect of having multiple versions of the same project, each with its own special quirks. Preprocessor directives are probably one of the most useful features built into the DM language, and probably one of the least used as well. That's why I wrote this article; I felt preprocessor directives needed a bit more attention, because once you start using them, you don't stop.

Table of Contents:


#define

The #define directive is used to define static names to equal a value. It can also take arguments (parameters). It's most useful to define constant values (such as lengths of delays, and other parameters you might want to fiddle around with while you're testing and balancing the game.) You can also use #define in your .dme file so that you'll have an easy way to store all your project's configurable parameters in one place.


Example of #define
#define NAME 1
var/name_value = NAME //now the variable name_value is set to 1

Example of #define with parameters
#define NAME(value) value*2
var/name_value = NAME(4) //since 4*2 = 8, name_value will equal 8

#include

The #include directive is used to manually include separate files into your project, and if the project is compiled as source code an "include.txt" file is created allowing the user to download any extra included code. Note that Dream Maker will automatically create #include directives in your .dme file for whatever files you've checkmarked in the file tree. (It can be stubborn about the order these appear in, which can cause problems if you declare a proc in one file and override it in another; if you ever need to overrule Dream Maker's judgment, use #include at the beginning of a .dm file to insert the file that has to be included first.)


Example of #include
#include "file.dm"  //This includes a locally found file called "file.dm"
#include <nadrew\somelib\somelib.dme>        //This includes a library found in the "lib" folder.


#error

The #error directive is used to output a compile-time error if certain conditions aren't met (this is ideal for libraries). It will cause the compilation to fail.


Example of #error
See #if


#if

The #if directive works a lot like the if() proc in DM, except it can be used to check if certain things have been defined. This can be useful for many purposes; one good example is creating "demo" and "full" versions of a project. It would be a pain to keep two very similar projects in two separate directories, and keep them synchronized, if the only real difference between the two versions is just the inclusion or removal of a few blocks of code.


Example of #if
#if defined(something)      //Checks to see if "something" is defined.
                #error "something" is defined -- compile failed!

                //Output a compiler error warning against defining "something".
#endif     //End the if statement (works like C and Visual Basic)
Just as a note, "defined()" is like a proc that's called by the #if directive, which is why it uses parentheses. "#ifdef" doesn't use parentheses because it's a whole directive in itself.

The #else directive shows a good example of how to use #if in a way other than "defined()"

#else

#else is reached when #if fails.


Example of #else
#define something 1
#if something != 1
//If "something" is not 1 compile correctly #else
#error Compile failed "something" is set to 1
#endif

#ifdef

This works exactly like #if defined(Macro)


Example of #ifdef
#ifdef Macro       //If "Macro" is defined
        #error "Macro" is defined!
#endif


#ifndef

#ifndef works exactly opposite of #ifdef; it will follow through if a macro name if not defined.


Example of #ifndef
#ifndef Macro      //if Macro is NOT defined
        #error "Macro" is not defined -- compile failed!
#endif


#elif

#elif is short for "else if". This means if the first #if fails it goes to the first #elif, and so on. (It's like #else, except it lets you add a new condition.)


Example of #elif
#define something 2
#if something != 1
#error Something is not one.
#elif something == 2 //If something is not 1 but 2, allow a proper compilation #else //if both conditions fail #error Both conditions failed! #endif

#endif

#endif indicates the end of an if statement.


Example of #endif
See #if