Loading Classes Dynamically in ActionScript 3

Loading classes dynamically in ActionScript 3 is easy, but there are a couple of tricks to keep in mind.

First of all, to load a class dynamically, use the flash.utils.getDefinitionByName function. Be sure to pass in the fully qualified class name, like this:

var MyClass:Class = getDefinitionByName("com.mydomain.package.MyClass") as Class;
var myInstance:MyClass = new MyClass();

Easy enough. But there’s something to watch out for. Class definitions only get compiled into your SWF if they’re declared somewhere in your code. In other words, if MyClass was never declared, the class definition wouldn’t be included in your SWF, and you would get a runtime error when trying to load it dynamically. (Note that this is not the case for classes native to the runtime — only your own custom classes.) Even importing the class isn’t enough to get it included — it must actually be declared.

There are two ways to work around this issue:

  1. Declare the class somewhere in the code.
  2. Use a command line argument to force the class definition to be included.

Below is an example of declaring the class before loading it dynamically:

MyClass;
// or
var mc:MyClass;
var MyClass:Class = getDefinitionByName("com.mydomain.package.MyClass") as Class;
var myInstance:MyClass = new MyClass();

This works fine, but in my opinion, it may defeat the purpose of loading a class dynamically. Usually you want to load classes dynamically because you’re not sure until runtime which class you’re going to want. If you knew which classes you wanted before you loaded them dynamically, you could just instantiate them directly.

The other option is to use the mxmlc -includes compiler argument like this:

-includes=com.mydomain.package.MyClass

Now, the class definition will be compiled into your SWF, and you can load it dynamically anytime you want without ever having to declare it.

To specify compiler arguments in Flash Builder, follow these steps:

  1. Right-click on your project.
  2. Choose “Properties”.
  3. Select “Flex Compiler”.
  4. Enter your arguments in the “Additional compiler arguments” section.

As an aside, you can use the flash.utils.getQualifiedClassName and flash.utils.getQualifiedSuperclassName functions to get the fully qualified class name of an object instance.

13 Responses to Loading Classes Dynamically in ActionScript 3

  1. Chris says:

    Awesome, I didn’t know about the Flex compiler option. I like that much better than having to reference the class inline. Is it possible to include an entire package using it? It could be nice for including variable objects when doing doing remoting.

  2. Rich Morey says:

    Can you use the compiler argument like this:

    -includes=com.mydomain.package.*

    to include all classes in a package?

  3. Christian Cantrell says:

    I’m pretty sure including all classes from a packages does not work, unfortunately. That would be a great improvement to the compiler.

  4. Pingback: Loading AS3 Classes at run-time - Almer/Blank Labs

  5. Ross R says:

    The thing I don’t understand is why you would do it this way. It seems to me that the logical purpose of dynamically loading classes is so that you can add a new class without recompiling the app and/or to keep the file size of the SWF down. But, if you are compiling the class into the SWF, then you have to recompile the entire SWF regardless.

    I think it is better to do it like you do for dynamic font loading. If you create a blank SWF that only has that class (or classes) compiled in, then load that SWF into your application, you can then use the classes, which have been loaded dynamically. Its all upside. You can have a smaller application SWF because the classes aren’t compiled in, allowing you to dynamically track the load of new classes. You can compile the application once and still make small changes and upgrades.

  6. Bart says:

    Another option is to compile an IInterface with your main application’s SWF, then compile the classes you want to load dynamically (and which are implementing that interface) into separate SWF’s, then loading those depending on a certain condition.

    After loading you grab the classes by their name from the ApplicationDomain of the loaded SWF, then make a new instance and cast them to your IInterface.

    Generating and updating your implementation-SWF’s could be automated with ANT and suchlike, and you can get clever with targeting specific ApplicationDomains but in essence this is more dynamic loading then what you mention in the article (since the actual implementation isn’t even included in your main SWF).

  7. Gregor says:

    You can also use Frame metadata to include the classes.

    Good link http://blogs.adobe.com/rgonzalez/2006/06/modular_applications_part_2.html

  8. Yakov Fain says:

    This is not exactly dynamic class loading as you have to list all the names you may load in advanced. We’ve described a technique of self-initialized libraries in this blog http://flexblog.faratasystems.com/2010/01/27/taming-flex-rsl as well as in the book Enterprise Development with Flex.

  9. Arnoud Bos says:

    Nice post, but as Yakov stated this is not dynamic class loading. The title of this blog post is misleading. Instead of “loading” i think “instantiation” or “creation” would describe what you are doing here.

  10. Pingback: Flastar » AS3 links №1

  11. Pingback: Omar Gonzalez » Blog Archive » Loading AS3 Classes at run-time

  12. Wimski says:

    How do you do a kind of ‘force import’ of custom classes in Flash CS5 instead of Flex?

  13. Tom Ortega says:

    Anyone know if you can use the includes option in an ANT or build script? If so, have an example handy?