Written by: Jesse Stratford
Time: 35 minutes
Difficulty Level: Beginner
Requirements: Flash 5 and MX
Topics Covered: LoadVariables and LoadVars Objects (Flash MX), from the ground up.
Assumed knowledge: Familiarity with Variables, Paths, Objects, Methods, Flash MX Event Model.

With the introduction of Flash MX and the push towards Flash as an application development medium, loading content on the fly is becoming ever more important. In the near future I'll be putting together a comprehensive intro to XML and Flash for the uninitiated, but for the time being I'll write up this quick tutorial on loadVariables as it's still invaluable, even (perhaps especially) in Flash MX.
This tutorial will assume you're using Flash 5 or above, and the later sections will cover Flash MX specific topics.

LoadVariables was introduced in Flash 4 but as with many other Flash 4 functionalities, only realized its full potential in Flash 5. In Flash MX things have been made even sweeter but let's not get ahead of ourselves.

LoadVariables allows you to load in data from an external source provided it is formatted appropriately. Most often this external source is a static text file, but that needn't be the case. Dynamically loading text content is great for making easily updatable, extensible web applications and sites. For instance, say you are developing a web site for a client with little or no technical knowledge, who does not own a copy of Flash but wants to be able to update their own website as easily as they might using MS Word or some other (yuppie) application. If you hard-code your text content, such as ‘Site News' or the like into the application, you've just lost yourself a client.

As mentioned above, Flash will spit the dummy if your target file isn't formatted correctly, so before we get into coding, let's explore the proper format for a file which we intend to load later on. The first thing to note is that the ampersand character (‘&') is used to denote the beginning of a new variable. A single equality symbol is used to separate the variable name from its value. So the following is valid:

&siteNews=Something amazing happened on the way to the nerd convention!

Note that spaces after the equals sign are perfectly legitimate, but try and stick them between the ampersand and the variable name, or after the variable name, before the equals sign, and you'll just cause yourself trouble. Be vigilant!

Most of the time we want to load more than one variable from a file. Remembering that the ampersand always precedes a variable name, that's done in the following manner:

&siteNews=Something amazing happened on the way to the nerd convention!&linkToStory=http://www.actionscript.org/

Note that I did not include a newline, or any white space characters at the end of the first variable's value, I just added an ampersand and began the next variable. Why? Because if you include a newline, or a few blank lines so your text file looks pretty in a text editor, they are, as far as Flash knows, part of your variable as well, so your variable will be a sentence including a whole stack of newline characters at the end. We'll deal with how to eliminate catches like this towards the end of the tutorial, in the section on Parsing.

Just to clarify, even though the above script is one long String, Flash is clever enough to see the ampersand and realize it's two variables, so once the loadVariables operation has completed, we'll be left with two variables named siteNews and linkToStory, with their respective values filled in.

The next most important thing to note about loadVariables in Flash is that everything is loaded in as a String. If you don't know what a String is, try reading the Variables tutorial. For instance, the following is perfectly legitimate, but may not have the desired effect.


So you load this into Flash and tried the following:

myVar = hoursInJessesDay + 5;

Any bets as to what that will output? No, not 34. It will output 295. Why? Because you loaded 29 in as a String, and the + operator for Strings just whacks the second argument after the first (concatenates). Don't despair though, there is hope. You can use the parseInt() or Number() methods to convert the String to a fully mathematically sound number for use in all your math operations, once it's loaded. We'll get to that in the swonking section.

So now you know the rules. Let's take a look at the code.

Many of you will have used loadMovie and will know that it comes in two flavors: loadMovie and loadMovieNum. Well, loadVariables is exactly the same. You can load in content using loadVariables or loadVariablesNum. loadVariablesNum accepts two arguments (generally); a file to load the values from, and a Level to load them into. As levels are annoying and bothersome I'm going to skip over this as much as possible. Suffice to say the following code will load a set of variables from file.txt into Level1:


Keep in mind variables loaded into a specific level will need to be referenced (from outside that level) using _levelX.variableName, where X is the level number. From within that level they can be referenced using _root. That's all covered in my Paths tutorial.

With that out of the way, let's get to my preferred instantiation: loadVariables. loadVariables also accepts two arguments; the first is again the name of the file to load but this time the second is the instance name of the movieclip which will accept the variables. Users of Flash MX should note that you can use loadVariables with the arguments you would normally use for loadVariablesNum and it will pick the correct operation for you. So, in it's most basic form, the loadVariables code to load variables in a file, file.txt into the timeline of an MC in the current movie with instance name ‘holder' would be:

loadVariables ("file.txt","holder");

Note that the second argument above is a String, surrounded by quotes. While it will often work if you make it a reference, that's not the best idea for various reasons. Also, just to bat you all over the head with the Object Orientation bat; it's probably wiser to write the above code in the following form:


This code does exactly the same thing as the code preceding it, but it's nice to read and more widely used. And better… I think.

Continued overleaf...