AS3 Conditional Compilation
From FlashDevelop
ActionScript 3 supports conditional compilation and FlashDevelop offers a nice integration of this feature.
Read: Using Conditional Compilation
FlashDevelop default constants
FlashDevelop automatically sets 6 constants:
- CONFIG::debug - true if "Debug" configuration is active
- CONFIG::release - true if "Release" configuration is active
- CONFIG::timeStamp - "24/12/2010" (ie. DD/MM/YYYY) as the date of build.
- CONFIG::air - true if the target platform is AIR or AIR Mobile
- CONFIG::mobile - true it the target platform is AIR Mobile
- CONFIG::desktop - true it the target platform is AIR
You may wonder why the timestamp does not contain the time: that's because setting a different value on each compile was causing serious memory leaks when using the Flex Compiler SHell. Maybe that's fixed in Flex 4 SDK, maybe not.
User-defined constants
In the Project Properties, Compiler Options tab, you can populate the "Compiler Constants" list.
Each line should contain a pair: NAMESPACE::variable_name,value with:
- NAMESPACE - any identifier,
- variable_name - any identifier,
- value - an AS3-valid constant value (true, false, 0, 1.5, "Company Name") or expression (1 > 2, 4 - 1).
Usage example
A Compiler Const can be used as an externally defined constant value or expression:
public const COMPANY:String = CONFIG::companyName;
private function someFunction() {
if (CONFIG::debug == true) {
trace("This code only executed in Debug configuration (but still compiled).");
}
trace("Built: " + CONFIG::timeStamp);
}
Or it can also be used to generate different code paths:
private function someFunction() {
CONFIG::debug {
trace("This code is NOT compiled in Release configuration");
return;
}
trace("This code never executed in Debug configuration - and apparently not even compiled");
}
CONFIG::debug
private function someFunction() {
trace("debug version");
}
CONFIG::release
private function someFunction() {
trace("release version");
}