Loading an SWC at runtime in different IDEs
Posted on 2010-12-03
Aah, the joy of being able to obtain code completion during development, what dev doesn’t like this? A while back, I explained how to link to SWCs to get code completion, but not compile said SWC in the SWF, so it could be loaded at runtime. It turns out that all IDEs do not necessarily behave the same way Flash Builder 4 does. Figures…
Ever since I went to Flash on the Beach, I have been playing with more IDEs than Flash Builder. I have to say I’ve been quite lucky: all attendees were given the FDT 4 Pure IDE. Also, I have been one of the lucky winners of an IntelliJ IDEA 9 licence. And since I was already trying all sorts of IDEs, why not look at FlashDevelop, an open source IDE, sadly available only on Windows.
When I look at the metrics of my blog, it seems that loading a SWC at runtime (along with the post about Facebook and AS3) is the most popular subject, so I though it would be better if I was more thorough and explain how to repeat the workflow mentioned above for all these IDEs. Obviously, if anybody knows even better ways to go about what I’m about to explain, by all means share with me.
Flash Builder 4
I already published a post about this workflow and this IDE, simply refer back to it.
When you want to add an SWC, I must say it’s quite simpler here. No need to go hunt in the preferences’s subcategories. Right-click on that SWC, the select “Source Folder > Add to Classpath”.
Now, the small issue that this presents, there is no prompt that asks whether you want to embed said SWC in your code, or to load it at runtime. I hunted a bit for this one, but in the end it takes s simple manual change. I was wrong on this one, as pointed out by Mattes Groeger:
[…] you can set this option via GUI. Just call the project “Properties” window by right clicking the project in the Flash Explorer view. Then in “FDT Build Path” click on the “Build Order” Tab. Here you can choose for each library via check box if it should be used as runtime library or not.
Alternatively, you can still go and play with the settings file, as I explain below.
Whenever you create a project in any Eclipse-based IDE, a .project file is created. With FlashBuilder, there is also a .actionScriptProperties. Do not worry if you have never seen these files, they are “invisible.” They are actually XML files that contain project configurations. I’ll probably have to come back to these sometime in the future.
I was then thinking, FDT probably creates those files too. Lo and behold, it does! Now, how do you edit these? At the top right of the Flash Explorer panel, there is a triangle pointing down. In there, select “ Filters…”.
From there you can select what types of file you want and do no want to see. Personally, I really dislike it when there are hidden files, so I want to see all of them. Once you have that in place, you will be able to find the .settings folder.
I mentioned the .actionScriptProperties for FlashBuilder, for FDT you want to open com.powerflasher.fdt.classpath. In there, all the sources paths are listed. In order to change an SWC so that its code is not embedded in the SWF, change the
useAsSharedCode attribute from
true. Then just make sure you load your SWF before you instantiate any class from that shared library.
Voilà for FDT.
Now for another beast altogether. I must say I have not been able to familiarize myself much with this IDE yet, and I find it a bit more complex than necessary. I mostly had to refer myself to Jesse Freeman’s posts about the AS3 in IntelliJ IDEA workflow to be able to set up and compile a basic project. Once that behemoth in the room is taken care of and you know where to add SWCs and sources as libraries, it turns out to be simple.
First, right-click on your project (which is called a module in IntelliJ IDEA) and select “Module Settings”.
In these settings, select your module and go to the Dependencies tab. If your SWCs are not there already, add them with–you guessed it!– the “Add” button. At the right of the path to that SWC, there is a combo box from which you can select options for the scope: “Compile”, “Test”, “Runtime” and “Provided”. The one we need to choose in this case is “Provided”.
You can now go about your business for the rest of your project.
Working with FlashDevelop is a bit tricky for me, since I’m on a Mac. However, back when I was working at an e-com shop 3 years ago, I was using FlashDevelop, I should be alright. What really help I have to say is that this tool has an active community, which saved me a great deal of trouble. A search on Google and I got this forum thread which answers all my questions. So, credit given when credit due, now let’s look at what is to be done with FlashDevelop.
First, select your SWC from your library, and from the contextual menu, choose “Add To Library”.
Once the SWC is added, the label will show in blue. Right-click again on it, and then the “Options” menu is visible.
In that window, you are presented with the necessary options. In this IDE as well the terminology changes, but at least a description is given. Check “External library (not included)”.
As you will see from the sources I provide at the end of the article, all these IDEs actually play along quite well. But if for one reason or another, you are more the I-don’t-depend-on-no-IDE kind of person, you can also compile your project directly into Ant. I know that after I have written my post about Ant, I should have written about how to compile with said insect, but since I faced the loading a SWC at runtime issue first when I started dabbling with FDT, I thought about writing about this workflow first.
Let’s get to business. In ant, you need to use the
runtime-shared-library-path tag so that when you compile your code is checked against your library, but that the library is not included.
Specify in the
path-element attribute the path to the SWC, and in the
rsl-url attribute of the
url tag against what SWF the SWC is compared.
This is actually the first time I try to do something and see how each IDE behaves. I’m quite happy to have found out that all of these play well together, that is good news to know that if you commit all of your project built in one IDE to SVN, that another developer can check it out and run it in another IDE without conflict.
However, one thing that did bother me is the amount of crap (the worst are Flash Builder and IntelliJ IDEA) that these idea create, there are so many useless files! And I am sorry for this, they are all part of the sources as well.
More related to the issue though, it is nice that all IDEs offer a way to work with code completion of assets at development time.