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.
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.
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.
#End
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!
Recent Comments