The other request I foolishly posted under bugs wasn't of particular use, however, it draws me to other useful C pre-processor commands, namely; #ifdef.
#ifdef allows values defined using #define to be queried, and will accordingly add or remove code to be compiled. For example:
#define DEBUG 1
default {
state_entry() {
#ifdef DEBUG
llOwnerSay((string)llGetScriptName() + ": " + (string)llGetFreeMemory() + " bytes of free-memory.");
#endif
// Some code
}
}
Aside from debugging (where this is massively helpful, especially if the defined values carry over to imported modules. I currently have two scripts I'm developing that are basically identical, but some small differences, #ifdef could allow me to build them more easily from a single large module, especially if combined with adding states it could be a great way to develop some very large scripts.
For the debugging use case, note that the optimizer will turn this:
integer DEBUG = FALSE;
...
foo() {
if (DEBUG) {
// some code
// ...
}
// some more code....
}
into
foo() {
// some more code...
}
i.e. because of constant folding and dead-code elimination, much of the #ifdef use case for debugging is handled.
Okay then, for debugging that would be fine. However, it doesn't allow me to strip code in slightly more complex ways.
For example, if I'm including a module that contains various core functionality between several scripts, then I can't simply toggle a variable to have the unnecessary parts culled, as that would affect all-scripts. And I can't define a variable in the script that's including the module, as the module isn't aware of the script's variables.
Currently what I've done is added some functions in my module for setting the behaviour, and the code has if-statements to switch or skip behaviours, but all the code is still included during pre-processing, it's just ignored at run-time. I suppose if the pre-processor could detect that I only ever set these variables at the start (default state_entry) of my script, then it could fold them normally.