Discover Master of Alchemy, our first iPad/iPhone and iPod touch game!

Follow Me


Forum's topics

Latest Files


Top Rated


Photo Gallery

How to use the LoadVars Class


The LoadVars class allows developers to have a better control of the external loaded data. There are many reason to use the LoadVars class intead of the old deprecated loadvariables and loadvariablesNum

Brief Description of LoadVars

Both LoadVars and loadvariables allow flash to communicate with external data source, but LoadVars offers methods, being a class, which the loadvariables doesn't support, being a global function.

Some of the benefits if the LoadVars:

1) availability of internal methods, in order to control the received/sent data, such as onLoad. the onLoad method is executed automatically (this means you don't need to make a call) once data is received from flash. Moreover the same onLoad method has an internal argument which tell us if data has been received or not (Example of a 404 page)

2) methods such as getBytesLoaded and getBytesTotal. We can know how much data we are loading.

3) all the data sent or received from the LoadVars class remains into the class itself. This is a big differences between the loadvariables function in which all the data received were stored in the same scope of the function (thus overwriting all the data in the same level). This offers again a better control on the data received

1. the .load() method

the LoadVars.load() method loads variables from an external source in URL-encoded format. Flash will read the output of the page you're loading. Once all the data is loaded, all the variables will be stored into the LoadVars instance which makes the call.

Example of url-encoded output (the only which flash can read using loadvars):


for more info about the format at

- a boolean value into the onLoad handler, which means if data has been received correctly.

A simple usage.

Let's create a simple php page like this:

$nome = "pietro" ;
$nick = "lana" ;
$mail = "" ;

print the variables to the output in order to let flash read them

$vars = "" ;
$vars .= "nome=" . $nome . "&" ;
$vars .= "nick=" . $nick . "&" ;
$vars .= "mail=" . $mail ;

echo $vars ;

Note the usage of "&". if you have more than 1 variable to send to flash you must concatenate the variables with the "&" (like the urlencoding using the GET method in html). Each time flash will read the & char it will assume that the following word it's a variable name.


will prduce in flash:

name = "pietro";
nick = "lana";

If you need to use the "&" char inside a variable text (for example name=Alex&me) you must urlencode the "&" value into %26 (name=Alex%26me)

To lad variables in flash:

/* first create a new instance of the LoadVars object */
myVars = new LoadVars();
// call the load method to load my php page
myVars.load(" http://percorso/pagina.php ");
// once vars have been loaded, we will have these variables:

myVars.nome    // "pietro"
myVars.nick      // "lana"
myVars.mail     // ""


- every variable received will be a string, this means that if you pass [ value=1 ] , once received we will have this:
myVars.value = "1;

- PAY attention of spaces before of after your code, it will be read!
for example if you write into a .txt file:

you'll have in flash: = "alex\r\n";
myVars.value = "20\r\n";
myVars.num = "1";

Another important consideration.
When working with php files, if you make a .load calls in flash like this:

you can't test your movie inside the flash IDE (CTRL+ENTER), this because in this way the php file won't be processed from your webserver and flash will read the source code of the php file, not the expected output!!

For this reason, while you're testing your movie inside the flash ide, use always absolute paths:
where localhost is just an example, it can be your domain name

( this is a common error )


2. the .onLoad method

When all the variables has been loaded and parsed by the LoadVars object, the onLoad method is executed, if defined.
Let's use a simple trace action to see if our variables has been loaded..

myVars.onLoad = function( ){
    trace("variables loaded");

An important thing to know is that the onLoad method is invoked from the onData method (another method of the LoadVars object) in this way:


As you can see it will pass the "success" parameter, a boolean (true/false) which means if the page is loaded correctly or not.

So, our code will be:

myVars.onLoad = function( success ){
        trace("variables loaded");
     } else {
        trace("Error loading page");

Now we can see how to access to the loaded variables.

myVars.onLoad = function (success) {
     if (success) {
        trace (" variables loaded ");
        for( var prop in this ) {
            trace (" key " + prop + " = " + this[prop]);
    } else {
        trace (" Error loading variables ");

Note the usage of "this" inside the myVars onLoad method. In fact while the onLoad method is executing everytime we use "this" we are refers to the myVars object itself, not the flash timeline !
All the variables are stored into the myVars object, so for this reason we need to use "this" to access to them.

But, if you want to access to the current timeline?

In order to refer to the timeline in which the onLoad methos is working you can do this:

myVars._path = this   /* we are outside the onLoad method, to "this" is the current timeline */
myVars.onLoad = function (success) {
     trace(this._path) // will output "_level0"
     if (success) {
        trace (" variables loaded ");
        for( var prop in this ) {
            trace (" key " + prop + " = " + this[prop]);
    } else {
        trace (" Error loading variables ");


a common error is try to access to the LoadVars properties when they are not already loaded. You can only access to its property only after the onLoad handler is executed, not before!

Example of wrong usage:

myVars._path = this;
myVars.onLoad = function(){ =
trace( // ERROR!!

correct usage:

myVars._path = this;
myVars.onLoad = function(){ =
    trace( // CORRECT