# (FMX) Load external and select random

**URL:** <https://forum.kirupa.com/t/fmx-load-external-and-select-random/26093>\
**Category:** flash\
**Created:** [July 19, 2003, 10:09am UTC](https://forum.kirupa.com/t/fmx-load-external-and-select-random/26093 "2003-07-19T10:09:05Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![chipdouglas](https://avatars.discourse-cdn.com/v4/letter/c/ecc23a/32.png) [@chipdouglas](https://forum.kirupa.com/u/chipdouglas)\
**Post date:** [July 19, 2003, 10:09am UTC](https://forum.kirupa.com/t/fmx-load-external-and-select-random/26093/1 "2003-07-19T10:09:05Z")

</div>

Hi there!

How do I load a external file and then display a random var (fact) from it??

this is what I got 🙂

## -------------AS-------------------------- loadVariablesNum(“data.txt”, 0);

## -------------external file------------- &fact1=hi there&fact2=hello

The number of facts in the external file is random, so some dynamic stuff would be cool…total newbie 🙂  
:beam:

/ChipDouglas

[http://www.chipdouglas.dk](http://www.chipdouglas.dk)

---

<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 19, 2003, 10:25am UTC](https://forum.kirupa.com/t/fmx-load-external-and-select-random/26093/2 "2003-07-19T10:25:58Z")

</div>

Use the LoadVars object instead of loadVariables/loadVariablesNum to load the TXT file.  
[AS]lv = new LoadVars();  
facts = new Array();  
lv.onLoad = function(success) {  
if (success) {  
for (var prop in this) {  
facts[facts.length] = this[prop];  
}  
} else {  
trace(“error”);  
}  
};  
ASSetPropFlags(lv, “onLoad”, 1, 0);  
lv.load(“data.txt”);[/AS]  
To get a random variable:  
[AS]var fact = facts[Math.floor(Math.random()\*facts.length)];  
trace(fact);[/AS]  
LoadVars object: [http://www.macromedia.com/support/flash/action\_scripts/actionscript\_dictionary/actionscript\_dictionary427.html](http://www.macromedia.com/support/flash/action_scripts/actionscript_dictionary/actionscript_dictionary427.html)  
Array object: [http://www.macromedia.com/support/flash/action\_scripts/actionscript\_dictionary/actionscript\_dictionary059.html](http://www.macromedia.com/support/flash/action_scripts/actionscript_dictionary/actionscript_dictionary059.html)  
Math object: [http://www.macromedia.com/support/flash/action\_scripts/actionscript\_dictionary/actionscript\_dictionary446.html](http://www.macromedia.com/support/flash/action_scripts/actionscript_dictionary/actionscript_dictionary446.html)

---

<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 19, 2003, 10:36am UTC](https://forum.kirupa.com/t/fmx-load-external-and-select-random/26093/3 "2003-07-19T10:36:55Z")

</div>

It does’nt seem to work…or i’m just confused…i’m a totally newbie so a .fla file would really help me 🙂

---

<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 19, 2003, 10:41am UTC](https://forum.kirupa.com/t/fmx-load-external-and-select-random/26093/4 "2003-07-19T10:41:01Z")

</div>

## -----------this works-------------------- lv = new LoadVars(); facts = new Array(); lv.onLoad = function(success) { if (success) { for (var prop in this) { facts[facts.length] = this[prop]; } } else { trace(“error”); } }; ASSetPropFlags(lv, “onLoad”, 1, 0); lv.load(“data.txt”);

## ----------this doesn’t---------------------- var fact = facts[Math.floor(Math.random()\*facts.length]; trace(fact);

get “undefined”

/ChipDouglas

---

<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 19, 2003, 10:43am UTC](https://forum.kirupa.com/t/fmx-load-external-and-select-random/26093/5 "2003-07-19T10:43:23Z")

</div>

You are just confused… 😛

Get the random variable **after** the external file has been loaded. That was just an example. :sigh:

---

<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 19, 2003, 10:49am UTC](https://forum.kirupa.com/t/fmx-load-external-and-select-random/26093/6 "2003-07-19T10:49:15Z")

</div>

_mumbling with a gun in my mouth_ plz help me!

---

<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 19, 2003, 10:56am UTC](https://forum.kirupa.com/t/fmx-load-external-and-select-random/26093/7 "2003-07-19T10:56:05Z")

</div>

I already told you how to do it, give me more details so I can help you better.

What do you want to do exactly with the variable?

---

<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 19, 2003, 10:59am UTC](https://forum.kirupa.com/t/fmx-load-external-and-select-random/26093/8 "2003-07-19T10:59:30Z")

</div>

i have a lots fact in sentence like “Copenhagen is the capital of Denmark”…I want them to be loaded from the external file a be random displayed in a dynamic text box…thats about it…does it make sence??

/Chip

---

<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 19, 2003, 11:24am UTC](https://forum.kirupa.com/t/fmx-load-external-and-select-random/26093/9 "2003-07-19T11:24:29Z")

</div>

[AS]lv = new LoadVars();  
lv.onLoad = function(success) {  
if (success) {  
var prop = null, facts = [];  
for (prop in this) {  
facts[facts.length] = this[prop];  
}  
**myTextFieldInstanceName**.text = facts[Math.floor(Math.random()\*facts.length)];  
} else {  
trace(“error”);  
}  
};  
ASSetPropFlags(lv, “onLoad”, 1, 0);  
lv.load(“data.txt”);[/AS]  
Just replace **myTextFieldInstanceName** with the actual instance name of your TextField.

---

<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 19, 2003, 11:29am UTC](https://forum.kirupa.com/t/fmx-load-external-and-select-random/26093/10 "2003-07-19T11:29:09Z")

</div>

in the words of Tony the Tiger: “Grrrrrrrrrreaat!” 🙂 it works and I found an error 😉 a missing “)” in

facts[Math.floor(Math.random()\*facts.length];

thanks a lot 🙂

---

<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 19, 2003, 11:32am UTC](https://forum.kirupa.com/t/fmx-load-external-and-select-random/26093/11 "2003-07-19T11:32:58Z")

</div>

A little typo. Yeah, I shouldn’t write the code in the quick reply box (although I wrote it correctly in my last reply). 😛

And 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 19, 2003, 1:15pm UTC](https://forum.kirupa.com/t/fmx-load-external-and-select-random/26093/12 "2003-07-19T13:15:22Z")

</div>

wtf is  
ASSetPropFlags(lv, “onLoad”, 1, 0);  
for?

---

<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 19, 2003, 2:13pm UTC](https://forum.kirupa.com/t/fmx-load-external-and-select-random/26093/13 "2003-07-19T14:13:24Z")

</div>

To hide the onLoad handler. Otherwise you’d also the store the function in the Array when the for…in action is executed. 😛

[http://chattyfig.figleaf.com/flashcoders-wiki/index.php?ASSetPropFlags](http://chattyfig.figleaf.com/flashcoders-wiki/index.php?ASSetPropFlags)

You could also use something like this:  
[AS]for (prop in this) {  
if (typeof this[prop] != “function”) {  
facts[facts.length] = this[prop];  
}  
}  
//  
for (prop in this) {  
if (!this[prop] instanceof Function) {  
facts[facts.length] = this[prop];  
}  
}[/AS]
