# Externally loaded Arrays? (is it possible?)

**URL:** <https://forum.kirupa.com/t/externally-loaded-arrays-is-it-possible/24951>\
**Category:** Uncategorized\
**Created:** [July 7, 2003, 3:52pm UTC](https://forum.kirupa.com/t/externally-loaded-arrays-is-it-possible/24951 "2003-07-07T15:52:42Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![weedallc](https://avatars.discourse-cdn.com/v4/letter/w/2acd7d/32.png) [@weedallc](https://forum.kirupa.com/u/weedallc)\
**Post date:** [July 7, 2003, 3:52pm UTC](https://forum.kirupa.com/t/externally-loaded-arrays-is-it-possible/24951/1 "2003-07-07T15:52:42Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 7, 2003, 4:17pm UTC](https://forum.kirupa.com/t/externally-loaded-arrays-is-it-possible/24951/2 "2003-07-07T16:17:34Z")

</div>

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]

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 7, 2003, 4:36pm UTC](https://forum.kirupa.com/t/externally-loaded-arrays-is-it-possible/24951/3 "2003-07-07T16:36:10Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 7, 2003, 4:38pm UTC](https://forum.kirupa.com/t/externally-loaded-arrays-is-it-possible/24951/4 "2003-07-07T16:38:57Z")

</div>

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 🙂

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 7, 2003, 6:42pm UTC](https://forum.kirupa.com/t/externally-loaded-arrays-is-it-possible/24951/5 "2003-07-07T18:42:32Z")

</div>

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]

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 7, 2003, 6:52pm UTC](https://forum.kirupa.com/t/externally-loaded-arrays-is-it-possible/24951/6 "2003-07-07T18:52:09Z")

</div>

OOOPS!!! :beam:

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

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 7, 2003, 6:55pm UTC](https://forum.kirupa.com/t/externally-loaded-arrays-is-it-possible/24951/7 "2003-07-07T18:55:06Z")

</div>

You’re welcome.😛

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 7, 2003, 7:48pm UTC](https://forum.kirupa.com/t/externally-loaded-arrays-is-it-possible/24951/8 "2003-07-07T19:48:55Z")

</div>

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().

:-\

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 7, 2003, 8:39pm UTC](https://forum.kirupa.com/t/externally-loaded-arrays-is-it-possible/24951/9 "2003-07-07T20:39:30Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 3, 2003, 8:29pm UTC](https://forum.kirupa.com/t/externally-loaded-arrays-is-it-possible/24951/10 "2003-11-03T20:29:33Z")

</div>

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]

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 3, 2003, 9:09pm UTC](https://forum.kirupa.com/t/externally-loaded-arrays-is-it-possible/24951/11 "2003-11-03T21:09:15Z")

</div>

> 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.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 3, 2003, 9:20pm UTC](https://forum.kirupa.com/t/externally-loaded-arrays-is-it-possible/24951/12 "2003-11-03T21:20:14Z")

</div>

> \*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:

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 3, 2003, 10:59pm UTC](https://forum.kirupa.com/t/externally-loaded-arrays-is-it-possible/24951/13 "2003-11-03T22:59:36Z")

</div>

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.
