Configure Your Applications with ConfigScript

We recently finished up a Flash application that was to be deployed on a CD ROM for distribution. The application was pretty simple (more art and animation than serious coding). But there were certain parts of the application that needed to be editable and configurable by the client without requiring the SWFs to be recompiled.

The client was a skilled Flash designer, familiar with basic ActionScript, but not looking to dig into XML config files or URL encoded strings to feed a LoadVars object just to change some basic configuration.

I was thinking how it would be cool if the client could just use their existing ActionScript knowledge to create a simple configuration file that would live on the CD. Using an #include file, we could update data, but it would require a recompilation of the SWF. In this case, that wasn't an option. We needed a Configuration Script that would resemble ActionScript syntax and still be usable in a meaningful way within Flash at runtime. So, we made ConfigScript!

The demonstration files used for this article are available here

ConfigScript is a class that loads and parses text data that resembles ActionScript and provides a simple way for the Developer to retrieve and use those values. I made copious comments in the class, so I won't get into the deep aspects of the class here. Instead, I'll demonstrate its use and document its public API.

ConfigScript uses EventDispatcher to manage events, so it automatically supports the methods dispatchEvent(), addEventListener(), removeEventListener(), and dispatchQueue(). In its current form, it supports the events "Data" (dispatched when the data has arrived and been parsed) and "HTTPStatus" (dispatched when an error or other status event occurs).

Its other public functions are:

ConfigScript.load(uri:String):Void

This method takes in a URI (path to a text document) containing basic ActionScript syntax assigning values to variables of any name.

ConfigScript.getItemValue(id:String):Object

This method looks up the value of a ConfigScript variable based on the name of the variable in the ConfigScript file. Note, the variable names can also include dot syntax, so you can set a ConfigScript property of an object as in myImage._x = 100.

ConfigScript.getBytesLoaded():Number

This method returns the number of bytes currently loaded.

ConfigScript.getBytesTotal():Number

This method returns the number of bytes to be loaded.

Using ConfigScript

To create a ConfigScript file, just open a text editor, create a new text file and write out the configuration variables in normal ActionScript type syntax, as in:


The above ConfigScript is intended to path an image that will be loaded dynamically in Flash and then position it on the stage. I saved this text file in the demo as "testConfig.cs".

In Flash, we can open a new FLA, read the config file, then load and position the image with the following code:


ConfigScript will read and parse the "testConfig.cs" file. Once the "Data" event tells you that it's ready, you can access all of the ConfigScript properties by name with the getItemValue method.

It makes it a little awkward for the developer (who would probably prefer to just use XML), but it makes the process much easier for the designer who may only know simple ActionScript. Anyway, it worked great in our project. The client was happy that they could use their existing ActionScript skills to edit their application configuration, so I thought I'd release it here in case anyone else comes across the same problem.

You can download the ConfigScript class (full of comments to explain what it's doing) along with a simple working demo here.

By Satori Canton - ActionScript.Com

Comments

Anonymous said…
Great!!!
But remember extract com/ folder (in ZIP file)into Classes/ folder of Flash 8 (c:\Program Files\Macromedia\Flash 8\en\First Run\Classes\).
samduy said…
Thanks! :)

Popular Posts