Hey everyone, here we are again with a new post on Haxe. This one is a follow-up on the previous post. This one concerns conditional compilation. Now, let’s get into the specifics and how it works. For more information on conditional compilation, you can find it here.

Haxe Conditional Compilation

Conditional compilation is a special characteristic of Haxe that allows you to have code that can, or cannot show up in the final output of your program. Let’s share an example from one of my plugins.

# Standard Build
-js dist/Luna_SocialSystem.js
# This will compile the code and make sure it follows the RPGMakerMV code base.
-D compileMV
-js dist/Luna_SocialSystemMV.js

As you can see in the above, there is a conditional compilation flag (-D compileMV); this was mentioned in the previous post on build files. This allows us to create a build specifically for RPGMakerMV when we compile our code. Now, this is how it looks in the build file, but we also have to make use of these flags in our actual code. Here’s an example from the LunaTea library.

extern class Window_Help extends Window_Base {
* The text that is displayed within the window.
* @protected
* @type {string}
* @memberof Window_Help
private var _text: String;
//This if compileMV creates a conditional compilation block
#if compileMV
* Creates an instance of Window_Help.
* @param {number} numLines
* @memberof Window_Help
public function new(numLines: Int): Void;
public function new(rect: Rectangle): Void;
* Sets the _text property of the window;
* this text will be displayed within the window.
* @param {string} text
* @memberof Window_Help
public function setText(text: String): Void;
public function clear(): Void;
* Sets the current item of the help window.
* @param {RPG.BaseItem} item
* @memberof Window_Help
public function setItem(item: BaseItem): Void;
view raw Window_Help.hx hosted with ❤ by GitHub

As you can see in the above, there are three special tags (#if, #else, and #end). These three tags are used to create conditional compilation in your code. Similar to an if else in programming if that flag is mentioned in the build file, it will be what you see in the output. As I mentioned before in the previous post, this allows you to make multiple flavors of the build of your game or project. So, if you needed a build for console vs PC; you may have console tags, PC tags such as #pc. Cool right? But that’s just the beginning of the benefits; there are other benefits for anyone using your code.

Availability Of Functionality

When you’re writing code using your library. You can create clear abstractions so that users know what functionality works only on specific platforms! The great thing is that since Haxe is a statically typed language, you’ll get a compilation error in Haxe before the code is compiled for the target platform. This allows for a higher degree of safety to reduce errors and allows you to keep the work for maintaining multiple platforms to a minimum with a single code base.

In my own code, I use this to make the code cross compile across game engines like in the above example, allowing me and others to write Haxe code that works on both platforms with few issues.


That’s the power of conditional compilation. Next time, we’ll get into another feature of Haxe that can take your code to the next level. Stay tuned!

%d bloggers like this: