Actionscript Tip: PHP-style Variable Variables

Variable Variables may be a different concept for the need and result I came up with, but Variable Variables is what lead to my re-discovery of the powerfully dynamic set(); function in actionscript.

Discovery
I have been working on a small Flash CS3 project that imports and displays information stored in XML.
Like many, who wouldn’t want their actionscript to be:

  • simple
  • clean
  • dynamic
  • scalable

… all to the synchronous XML data and design requirements that might change down the road?

Everything was going fine until I hit a little roadblock in my XML parsing loop.
How do I get flash to create variably-named variables?

I found out my solution after some intensive research, here’s how …

One possible way is to use array referencing. But let’s remember, I wanted my script to be simple.

Then I thought about how Variable Variables in PHP would really help this situation.
Here’s a code example:

$a = "hello";
$b = "a";
$c = $$b; // c = "hello"

I searched high and low on the internet regarding Variable Variables in actionscript.
Some forums came close to what I was looking for and suggested this code:

var a = "hello";
var b = "a";
var c = eval(b); //c = "hello"

However that didn’t help me much as I was still not creating the variables with variable-names.
I was creating variables with set-names that I predefined in my actionscript code.
I need the code to create variables with names that come from an externally instantiated value.

I ended up going back to the Adobe Flash reference dictionary and looked up the entire scope of variables in actionscript. It lead me back to this familiar, yet rarely used function implemented way back in flash 4: Set Variable.

set("variablename","value"); // where the variable: variablename = "value"
OR
set(variablename,value); // where variablename = a variable value

Take a look at the following example code to see how Set Variable works:

var myXMLnodename:String = "peaches"; // a standard variable declaration
trace(myXMLnodename); // returns: peaches

// this is the same as:

set("myXMLnodename":String, "oranges"); // setting a variable using set(); function
trace(myXMLnodename); // returns: oranges

// continuing with that,.. create a Variably-Named Variable
// based on the value within myXMLnodename

set(myXMLnodename:String, "a citrus fruit");

// the last value of myXMLnodename was "oranges"
trace(oranges); // returns: a citrus fruit

// now the variable 'oranges' has been declared
// if the variable oranges is required, it will exist in the actionscript variable tree

Using this code concept inside of a for loop, helped me achieve what I wanted exactly.

Here’s my final code:

function createXMLvariables(xml_file) {
var mainNode = xml_file.childNodes;
for (var i = 0; i<mainNode.length; i++) {
if (mainNode[i].nodeName == “main”) {
var mainChildNode = mainNode[i].childNodes;
for (var j = 0; j<mainChildNode.length; j++) {
set(mainChildNode[j].nodeName, mainChildNode[j].attributes.textValue);
}
}
}
}

I guess Adobe still felt that this little old function still has some very important use.
Thanks for not deprecating this function guys!

No comments yet.

Write a comment: