Externally loaded Arrays? (is it possible?)

I would like to load an Array from a file into flash. I need it because I need to use a random element from that array at various times and have a variable value as the the index for which element I want to use.

Is is possible to get an array from a file? Or am I just dreaming?

Well you can’t dynamically load an array itself, but you can parse it into an array.

An example would be something like this…

TextFile (named array.txt for this example, but can be whatever) has…

myArray=1,2,3,4,5,6,7,8,9,0

On a frame in flash… [AS]myLV = new LoadVars();
myLV.onLoad = function(success){
if (success){
numArray = this.myArray.split(“,”);
} else {
trace(“file failed to load”);
}
myLV.load(“array.txt”);[/AS]

This loads the array.txt file and when it loads it parses the myArray variable into an Array by splitting it at the “,” symbol. So this will essentially give you an array that looks like this… [AS]numArray = [1,2,3,4,5,6,7,8,9,0][/AS]

Ah, I see, I never even thought about that. I was too caught up on trying to force it into Flash AS an array. Thanks a lot for the help.

You can use any symbol to split at BTW. I know a lot of people use | and some use stuff like :: to seperate their content. Of course you set that in the this.myArray.split() line :slight_smile:

Wait a minute… shouldn’t that be

[AS]myLV = new LoadVars();
myLV.onLoad = function(success){
if (success){
numArray = this.myArray.split(",");
} else {
trace(“file failed to load”);
}
}
myLV.load(“array.txt”);
[/AS]

OOOPS!!! :beam:

I missed a “}” to close out the onLoad. Thanks njs.

You’re welcome.:stuck_out_tongue:

ok, well, I was using the loadVariables() function before, and having no problem. I tried to do it your way, and it seems like nothing much is happening. Per your example, do I have to refer to variables after they are loaded as

myLV.numArray ?

Otherwise, I can’t figure out what’s wrong, I didn’t change any other code, just added the onLoad and used a LoadVar to call the load() function instead of doing LoadVariables().

:-\

No, it wouldn’t be myLV.numArray, it would just be numArray (assuming you are calling it from the same timeline).

It worked fine for me…

[AS]myLV = new LoadVars();
myLV.onLoad = function(success) {
if (success) {
numArray = this.myArray.split(",");
trace(numArray);
} else {
trace(“file failed to load”);
}
};
myLV.load(“array.txt”);[/AS]

I added the trace just to check and make sure the array was created, worked fine.

Am I right that in this case I can’t use numArray anywhere else than inside the onLoad function? Do I have to write everything where I need the numArray (the whole program) inside onLoad function or is there some other way?

Also in this case why do I get “Hi” before “Hello”?
Doesn’t myLV.load call onLoad wich outputs Hello and Hi comes after that.

[AS]
myLV = new LoadVars();
myLV.onLoad = function(success) {
numArray = this.myArray.split(",");
trace(“Hello”);
};

myLV.load(“array.txt”);
trace(“Hi”);
[/AS]

Am I right that in this case I can’t use numArray anywhere else than inside the onLoad function?
Now you can use it AFTER the onLoad function, but since it is created when the file loads, you can’t use it before that.

Also in this case why do I get “Hi” before “Hello”?
All code in the frame is interpreted before your file loads, so “Hi” will be interpreted and displayed before “Hello” since “Hello” waits until the file is loaded.

*Originally posted by lostinbeta *
**Now you can use it AFTER the onLoad function, but since it is created when the file loads, you can’t use it before that.
**

So, is the solution to use numArray not until the next frame? Sorry, I’m not yet quite familar with the structure of Flash. :hangover:

Well if you are able to do what you have to do in the onLoad, I recommend doing it then. You can call it in the next frame, but only if you play that frame after the onLoad is complete.

Depending on what modem the user has and the speed of your server, the loading time of the file will be different so running your code in the onLoad is really your best option.