Using a Pre-processor with ActionScript to define constants

Yesterday I posted some simple info on how to use the cpp pre-processor to pre-process ActionScript files. I showed how you can use the #ifdef directive to conditionally add / remove stuff from the final ActionScript file. However, cpp can do a lot more than just conditional section includes.

You can use the #define directive to define constants within your ActionScript file / class.

For example, lets look at a simple example:


[code]class ObjectCreator{var DEFAULTWIDTH:Number = 5;var DEFAULTHEIGHT:Number = 5;function ObjectCreator(){}function createObjects(Void):Void{for(var i:Number = 0; i < 10000; i++){var o = new Object();o.width = this.DEFAULTWIDTH;o.height = this.DEFAULTHEIGHT;}}}[/code]

In this case, we use a naming convention (all caps) to denote a variable that should be treated as a constant. However, it is not a true constant. This means that it can be changed at runtime, and that the player has to look up the value each time it is accessed. In certain circumstances, this can have an impact on performance.

However, if you run the ActionScript file through the cpp processor then you can use the #define constant to essentially allow you to use constants in your code.

Here is the same code using #define:

[code]#define DEFAULTWIDTH 5#define DEFAULTHEIGHT 5class ObjectCreator{function ObjectCreator(){}function createObjects(Void):Void{for(var i:Number = 0; i < 10000; i++){var o = new Object();o.width = DEFAULTWIDTH;o.height = DEFAULTHEIGHT;}}}[/code]

After running through the cpp pre-processor:

cpp -P ObjectCreator.p ObjectCreator.as

the following ActionScript file is created:[code]class ObjectCreator{function ObjectCreator(){}function createObjects(Void):Void{for(var i:Number = 0; i < 10000; i++){var o = new Object();o.width = 5;o.height = 5;}}}[/code]

Notice that DEFAULTWIDTH and DEFAULTHEIGHT have been replaced with their constant values of 5. Of course, in this case the constants are only being used in one place, but the more the constant is used, the more useful using #define becomes.

Couple of notes. Pay attention to the syntax for #define:

#define name value

There is no = sign between the name and value. There is also no trailing semi-colon. If you add either, they will be treated as part of the macro and inserted into your code

You can find more info about using the cpp preprocessor with ActionScript here. You can find more info about the #define directive here.