Compile time variables in Flash Develop

That is something that I have been wanting for a long time but I just found out how to do it.

So what is a compile time variable?

It is a variable that you can do conditions on at compile time (if) and the compiler will only include a set of lines in the resulting swf. This can be usefull for debugging purpose or when you have multiple languages or versions in a site. Let’s look at an example:

if (CONFIG::LANG == "en"){
  _pageHolder = new PageHolderEn();
} else {
  _pageHolder = new PageHolderFr();
}

In the previous code, if the compile time variable CONFIG::LANG is equal to “en”, than the line “_pageHolder = new PageHolderEn();” will be included in the final swf, otherwise it will be the other line. You can give a Boolean, int and String value (and probably other types too) to the variable.

So how do you set the value of that compile time variable?

Well there are a couple of examples out there about how to do so in Flash CS4 IDE or in Flash(Flex) Builder, but none in Flash Develop so that is what I am going to explain here. First if you do this in Flash Develop, it means you are either doing a Flex project or an ActionScript project (meanin you are compiling with the Flex compiler (is it still named like this?)). So once you have created your project, go in the Project Properties and in the Compiler Options tab, look for the Additional Compiler Options. Here is a screenshot of what it looks like:

compile-time

What you need to add in there is this:

-define+=CONFIG::LANG,"en"

Where CONFIG::LANG is your variable name and “en” is the value as a String. ¬†For the variable name it seems to always be starting with CONFIG::, I don’t know if that is a convention or if that is needed so that the compiler knows its a compile time variable. I will need to do more reasearch on this. If you wanted to set the variable to a boolean you’d do this:

-define+=CONFIG::LANG,true

And same principle for a int, you’d put just the number without the quotes.

Now in Flash Develop, the project properties are stored in a file with the .as3proj extention so if you don’t want to have to change the Additional Conpiler Option you can just copy the project file and rename it. In the new file change the compile time value and depending how you want to compile, you open the right project file.

Well, it’s as simple as this.

, , , ,

  1. #1 by one giant media - February 4th, 2011 at 20:01

    This is a good way to flip a TEST_MODE boolean variable true/false to signify if you’re compiling for testing on your local machine and thus should apply full URLs or compiling for the live environment where you want all URLs to be relative. I do this all the time and can’t imagine my workflow without it (AS3 codebases with upwards 100 classes taking parameters that change based on this compile time var)

(will not be published)
Subscribe to comments feed
  1. No trackbacks yet.