(FMX) Load external and select random

Hi there!

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

this is what I got :slight_smile:

-------------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 :slight_smile:
:beam:

/ChipDouglas

http://www.chipdouglas.dk

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
Array object: 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

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

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

You are just confused… :stuck_out_tongue:

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

mumbling with a gun in my mouth plz help me!

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?

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

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

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

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

thanks a lot :slight_smile:

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

And you’re welcome. =)

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

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

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]