#DEFINE / #IFDEF equivalent in Inform 7

Is there a way to conditionally compile code in the story file. At the moment I have many “if debugging, say " test”" type statements all over the place, but I’d like a way force these lines to never be part of the runtime code if ‘debugging’ is false.


You can use Daniel Stelzer’s Debugging extension, which allows you to write “debug say”–this is actually something you can set at run-time by typing “debug” at the command prompt (kinda like “rules” or “actions”) but it will print the line if debugging is on and won’t print it if it’s off.

You could also try use options (see section 27.18 of Writing with Inform). You could define a “debug say” phrase that prints something if and only if your “use debugging” option is active. Then you can remove the “use debugging” line when you’re ready. The use options actually translate into I6 as #IFDEFs, I think.

Or I guess you could include two sections in your code, one for release only and one not for release. In the for-release-only section “debug say” does nothing, in the not-for-release one “debug say” prints something.

The only one of these I’ve done myself is to use Daniel’s extension. (At the time there was an issue where typing “all” turned the debugging option on–also I had to include a line in a for-release-only section preventing the player from typing “debug”–but I think Daniel has fixed that.)

Yes, you can prevent those statements from compiling at all. Do something like this:

[code]Use inline debugging translates as (- Constant INLINE_DEBUG; -)

To #if utilizing inline debugging:
(- #ifdef INLINE_DEBUG; -)

To #end if:
(- #endif; -)

To debug (T - a text):
#if utilizing inline debugging;
say T;
#end if.

You can turn this on and off by including the use option, e.g.:

Use inline debugging.
Instead of jumping:
	say "You jumped!";
	debug "--Jumping test successful".

Erik, what’s the difference between that and this?

[code]Use inline debugging translates as (- Constant INLINE_DEBUG; -)

To debug (T - a text):
if the utilizing inline debugging option is active;
say T.[/code]

Matt, the difference is that the text itself isn’t compiled in my code, whereas in yours it is. It’s a minor difference really, since I wrapped the debug text in a say phrase for convenience: the say routine in my “to debug” phrase will execute in all cases, though it prints nothing if the debugging option isn’t on. But if you truly want no compilation, you can use the same technique more verbosely:

Instead of jumping: #if utilizing inline debugging; say "This line won't be compiled if the debugging option isn't in force."; #end if.

Make sense?


Another question: does this mean that the text gets compiled from I7 to I6, and then does the I6 get compiled to, well, whatever that gets compiled to? I ask because we encountered a syntax error wrapped in an #IFDEF in Disambiguation Control that led to a compilation error when the use option was active, but not when it was inactive, which made me think that the I6 compiler was just skipping everything inside the #IFDEF when it didn’t apply. (Also that no one had ever tried to use that option before.)

Everything will pass through I7 compilation, but any code between the #ifdef and the #endif will be ignored (discarded) by the I6 compiler unless the use option is active.

Thanks, Erik - that did the trick.

Another option for some circumstances is to use (for use with …) section headings which depend on extensions. If they don’t match then nothing gets compiled.

You can also modify Erik’s way to use the built in DEBUG constant, which means that testing and release builds will have the debug code included or excluded automatically.

1 Like