Load a text file into an array

15 posts

Flag Post

Okay, I’m trying to load a text file using LoadVars() in AS 2.0 and whenever I try putting information from the onload function into an array outside the onload function, the array loses scope so the data goes down the toilet. Any advise on how to load a text file into a global array?

 
Flag Post

put your array declaration outside the onload function…

var myArray:Array();

onload = function() {
//Do you stuff with myArray
}

 
Flag Post

Already tried that. Didn’t work. Apparently one way is to use the Delegate function. Unfortunately, my flash coding isn’t that strong. It’s driving me nuts because it’s so easy to do it in a lot of other languages. Also, AS 3.0 seems to have easier ways to do it. Unfortunately, I can’t switch until after the semester is over.

 
Flag Post
my_lv.onLoad = Delegate.create ( this, myParseMethod );
 
Flag Post

Yeah I’ve run into this problem before. Sometimes you need to create a scope variable… at least that’s what I call it. It’s kind of a hack, but you could try something like this.

If you’re inside a class, do this:

var scope:Object = this;

or on the timeline:
var scope:MovieClip = this;

Now you can write your onLoad handler function, and force it to scope properly.


var myArray:Array = new Array(); //note how I declare the array
var textLoader:LoadVars = new LoadVars();

textLoader.onLoad = function() {
trace(scope.myArray);
}

Notice how I’m declaring the Array and the LoadVars… the example that underseige posted is really not the correct way. AS 2.0 is pretty lenient, but still, I would say that use of the new operator is encouraged. If you want to initialize the array, do it like this:

var myArray:Array = [“fred”, “taco”, 1,2,3];

 
Flag Post

Holy crap that’s clever. I’ll give it a shot and thanks for idea.

Yeah, I try to use new as much as I can because it feels weird if I don’t, because I learned JAVA and some C#.

So far, if I populate items into the array and then load the onLoad function, I can read those items. However, if I add stuff within the onLoad function, it’s not showing up outside the onLoad YET it gets added and you can view it inside the onLoad function. Geez is this messed over.
I’ll try a few hypotheses to try to get around this. I wish actionscript had pointers.

I just tried this with a single string. Didn’t work. I’ll give Delegate another shot.

 
Flag Post

Can you post the code so i can take a look at what you’re doing?

 
Flag Post
stop();

// based off http://www.flash-creations.com/notes/servercomm_textfile.php

var answerList:Array = [];//new Array();
var d:String;
	
var my_lv:LoadVars = new LoadVars();

my_lv.onLoad = function(success:Boolean) 
{
	trace("answers.txt successfully loaded: "+this.loaded);
 
Flag Post
var i:Number = 0;
	
	if ( success == true )
	{
		while (this["ans"+i] != undefined )
		{
			d = eval( "this.ans" + i );
			//trace(d);
			_root.answerList.push(d);
			i++;
		}
	}
};

answerList.push("Hello World!");
answerList.push("Test!");

my_lv.load("./answers.txt");
trace(answerList);
gotoAndStop(2);
 
Flag Post

btw, you can use the <pre> </pre> tags to format your code on the forums here ;)

Seems like this code should work if you’re using Flash 8, and the code is on a frame on the main timeline:


//assume the text file’s contents are
// joe_taco_1_2_3

var scope:MovieClip = this;
var arr:Array = new Array();
var lv:LoadVars = new LoadVars();

lv.onData = function(thetext:String) {
scope.arr = thetext.split(“_”);
}

lv.load(“thetextfile.txt”);

trace(arr1); //should trace: taco

 
Flag Post

Ah, much better. Thanks. @mods: feel free to combine the two posts. Oh yeah, this is in a timeline.

Ooh. I’ll give that a try. For some reason, my code did not work at all on either Flash 8 or Flash CS3 using Actionscript 2.0.

Well, I’m getting about the same thing… Tracing thetext works within onData but tracing out of scope doesn’t. I’ll have to try that on Flash 8 on Monday. (I have Flash CS3 at home but Flash 8 is on the school PCs).

 
Flag Post

Oh yeah…

Sorry man I included the trace statement too soon. It’s the funny thing about loading external content: you need to wait until it’s loaded before the array gets defined.

OOPS! The actual code would look more like this:


var scope:MovieClip = this;
var arr:Array = new Array();
var lv:LoadVars = new LoadVars();

lv.onData = function(thetext:String) {
scope.arr = thetext.split(“_”);
}

lv.load(“thetextfile.txt”);

this.onEnterFrame = function() { trace(arr1); }


It will trace undefined until the data is loaded and parsed (via onData handler), and then it will trace correctly: taco.

 
Flag Post

No probs. I wish I thought of putting the trace on onEnterFrame. I need to stop thinking in C++. Unfortunately, looks like the frame is on an infinite loop.

 
Flag Post

Infinite loop avoidance :p


var scope:MovieClip = this;
var arr:Array = new Array();
var lv:LoadVars = new LoadVars();
var init:Boolean = true;

lv.onData = function(thetext:String) {
scope.arr = thetext.split(“_”);
}
stop();

lv.load(“thetextfile.txt”);

this.onEnterFrame = function() {
if ( init == true )
{
if ( arr1 != undefined )
{
init = false;
trace(arr);
}
}
}

 
Flag Post

Awesome. I just tried messing with some code and this will allow me to do exactly what I wanted to do, especially with future projects.