Dynamically created variablenames

Is it possible to have dynamically created variablenames in AS?

What I was thinking was something like this:

for (i = 0; i < 5; i++) {
“myVariable_” + i = “Whatever”;
}

After the loop is finished I want to have five variables named myVariable_1, myVariable_2, and so on.

The problem is that I cant figure out how to acomplish this.

“myVariable_” + i = “Whatever”;

…does not create a variable named myVariable_1.

eval(“myVariable_” + i) = “Whatever”;

…doesnt seem to work either.

Any one have the solution to this, please?

for(ii=1; ii<=5;ii++){
    _root["info"+ii] = variable
}

gotta use the _root array…

btw you dont HAVE to use _root… can be whatever scope you’re currently in (ore really any scope you want)

this[“info”+ii] = variable

you can also use the set function

set (“info”+ii, variable);

Thanks a lot, Jubba :smiley:

Unfortunately, when one problem is solved, another one appears…

It seems that the variables that are dynamically created this way, are also dynamically deleted (perhaps at the same time as the other dynamic variable is created).

The problem now is that I need all the created variables to remain active at the same time, since they are loadVars-variables, with which I load text from textfiles to textfields.


for (i = 1; i < 5 + 1; i++){
root["loadText" + i] = new loadVars();
root["textFil" + i] = “MyTextFile_” + i + “.txt”;
root["loadText" + i].load(root["textFil" + i]);
root["loadText" + i].onLoad = function() {
_root[“root.txt” + i].html = true;
_root[“root.txt” + i].htmlText = this.my_text;
}
}

Well, the above code seem to actually load the text into the textfields for a second or so, but when the next loop kicks in, the text unloads again.

thanks sen. When I was trying to do something similar I had to use _root[] to get it to work. so i assumed… :slight_smile:

Not sure about the new problem tho…

Thanks again for the help, you two :slight_smile: It taught me a couple of new AS-concepts.

However, Im a bit unclear as to how…

a) set(“info”+ii, variable);

…and…

b) this[“info”+ii] = variable;

…actually work.

I mean, if I first initiate a variable with method a), how do I then refer to it? If I use…

set(“info” + ii);

…wont I just create a new variable with the same name, but without any content (ie. clearing it, instead of refering to it)?

Do I have to use method b) to refer to the variable?

set is a top level function that sets a variable with the given name (which can be dynamically defined) in the current major scope and assigns it the value passed.

set(“variableName”, value);

is the same as

variableName = value;

because its a ‘top level function’ set is only specific to the scope in which its called… the ‘major’ scope. By this I mean if you are writing the script in the main timeline, the variable will be set in _root no matter what. You cant alter the scope at which set is set unless you write it directly within that scope. This means set only sets variables in movieclip objects since thats the only place you can physically write script. Within prototypes or within the function/method scope of other objects, set will still write the variable in the ‘major scope’ - the scope at which the script, as a whole, exists.

this[“info”+ii]

uses associative array referencing. Its just a way of referencing a variable (any scope) using strings (and therefore more dynamially) For more on that you can go see

http://www.kirupaforum.com/showthread.php?s=&postid=85182#post85182

I feel a bit like GW Bush when reading your post (dyslectic, that is…or maby just plain stupid :wink: ), but if I understand you correctly I should be able to create two loadVars -variables (in my code above) with the set -method that will be active (ie. not deleted when the loop enters another loop) the whole time, and then just reference them with the _root[] -array?

Something like this, perhaps?

for (i = 1; i < 5 + 1; i++){
set(“loadText_” + i, new loadVars());
set(“textFil_” + i, “MyTextFile_” + i + “.txt”);
root["loadText" + i].load(root["textFil" + i]);
root["loadText" + i].onLoad = function() {
_root[“root.txt” + i].html = true;
_root[“root.txt” + i].htmlText = this.my_text;
}
}

This way, two persistent variables should be created at each loop (“loadText_i”, and “textFil_i”), or have I missed something?

EDIT: Well, I must have missed something, since I cant get this to work.