Tiffany B. Brown

Clearing LoadVars() in ActionScript

The LoadVars() object is a funny thing. It’s basically a bucket for variables that you’d like to load into and send from an SWF file, as well as any methods and event handlers you invoke.

LoadVars() can be problematic, however, if you ever find yourself reusing the same object for repeated load() or sendAndLoad() operations. As explained in the ActionScript 2.0 Language Reference:

Downloads variables from the specified URL, parses the variable data, and places the resulting variables in my_lv. Any properties in my_lv with the same names as downloaded variables are overwritten. Any properties in my_lv with different names than downloaded variables are not deleted.

In other words, if you continue reusing the same LoadVars() object, you’re going to have more data floating around than you may need at a time. Let’s take a look.

First we’ll request a file containing variables with a load() operation.

var bucket:LoadVars = new LoadVars();
bucket.load('vars.txt');

The file vars.txt looks like this:

content1=The+quick+brown+fox&content2=Jumps+over+the&content3=lazy+dog.

Each variable (that bit to the left of the = sign) becomes a property of your LoadVars object (bucket), and each variable’s value becomes a the value for that property.

If we iterate through bucket using a for loop when the variables are loaded, we’ll see that content1, content2, and content3 are now properties of bucket.

bucket.onLoad = function(){
     for(i in bucket){  trace(i+': '+bucket[i]+"\n"); }
}

Outputs:

content1: The quick brown fox
content2: Jumps over the
content3: lazy dog.
onLoad: [type Function]

Now let’s say we need to send a variable to our server and retrieve some new results. We’ll do a sendAndLoad() operation, and store the results in the same bucket object.

bucket.username = 'cleophus';
bucket.sendAndLoad('morevars.txt',bucket,"POST");

The data received from morevars.txt looks like this:

firstname=Dwayne&lastname=Wayne&major=math

If we execute another for loop, once the data is loaded, we’ll now see that there are more properties stored in LoadVars().

bucket.onLoad = function(){
     for(i in bucket){  trace(i+': '+bucket[i]+"\n"); }
}

Now outputs:

content1: The quick brown fox
content2: Jumps over the
content3: lazy dog.
username: cleophus
firstname: Dwayne
lastname: Wayne
major: math
onLoad: [type Function]

Any more load or sendAndLoad operations will only add properties to the object unless you overwrite them — or delete them.

Before deleting anything from your LoadVars object, however, copy the properties and values to another variable. Arrays and objects are a good fit here. You can create an array that only copies the variable-value pairs that you need to keep, then delete each variable from the LoadVars object, emptying it for reuse.

function turnVarsToArray(sendAndLoadObj:LoadVars):Array{
	var loadVarsArr:Array = new Array();
	for(var e in sendAndLoadObj){
		if( typeof(sendAndLoadObj[e]) !== 'function'){
			var pair:Object = {vari:e, val:sendAndLoadObj[e]};
			loadVarsArr.push(pair);
		}
          delete e;
	}
	return loadVarsArr;
}

4 Responses to “Clearing LoadVars() in ActionScript”

  1. I understand that you have very serious reasons why you are still using ActionScript 2.0. I blame Macromedia/Adobe first before I would ever blame you.

    For the long term, you might want to look at “My First Flex 2 Framework” because my use of LoadVars() would be completely replaced with the E4X support in Flex/AIR.

  2. I understand that you have very serious reasons why you are still using ActionScript 2.0. I blame Macromedia/Adobe first before I would ever blame you.

    For the long term, you might want to look at “My First Flex 2 Framework” because my use of LoadVars() would be completely replaced with the E4X support in Flex/AIR.

  3. tiffany says:

    Thanks Bryan. My “very serious reason” in this case was “I had three weeks, and I am not a hard core AS developer.” I know just enough to get deep enough to run into issues like this :-).

    I’ll check out “My First Flex 2 Framework,” as well. Looks interesting.

  4. tiffany says:

    Thanks Bryan. My “very serious reason” in this case was “I had three weeks, and I am not a hard core AS developer.” I know just enough to get deep enough to run into issues like this :-).

    I’ll check out “My First Flex 2 Framework,” as well. Looks interesting.