Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

LoadVars problem

Status
Not open for further replies.

Leozack

MIS
Oct 25, 2002
867
GB
Hi all - I've managed to get flash to trace the variables from my test file which was in the same directory as the working folder using :

myVars = new LoadVars();

myVars.onLoad = function (success) {
if (success) {
trace (" variables loaded ");
for( var prop in this ) { trace(" key " + prop + " = " + this[prop]); }
} else {
trace (" Error loading variables ");
}
}

However - nothing I can do seems to give me access to the vars besides that trace. I thought I could get to them like this throughout my code

testvar = myVars.you;

But no luck it seems. How can I get them into global variables?

Also, is there an easy check to compare whether arguments were sent via the url query or via POST from a form or something? So I could check the url and if they didn't send the arguement in that check the POST data, or vice versa.

Thanks guys. Never used flash for external stuff before.

_________________________________
Leozack
Code:
MakeUniverse($infinity,1,42);
 
> I thought I could get to them like this throughout my code
> testvar = myVars.you;

Yes you can. But if you want to access it from another MovieClip for example, you have to target it properly e.g. _parent.myVars.you

Kenneth Kawamoto
 
Ok here's all the code I'm testing with. The end result is the variabels I pass in are traced but if I try to do something with them, like put them in a textbox for example, it ends up blank.

Code:
// Called with ?ConfigFile=path/to/filename

testvar = ConfigFile;

/* first create a new instance of the LoadVars object */ 
myVars = new LoadVars(); 

// call the load method to load text file 
//myVars.load(ConfigFile + fExt);//"[URL unfurl="true"]http://localhost/test/test/test.txt");[/URL]

// This seems to work with just test.txt
myVars.load("test.txt");

// test.txt contains just "&test1=hell&test2=goodbye"

//textbox = text;
//textbox = text.text;

textbox.html = true;

myVars.onLoad = function (success) {
     if (success) {
        trace (" variables loaded ");
        for( var prop in this ) { trace(" key " + prop + " = " + this[prop]); }
		//
		textbox.htmlText = "hmm " + myVars.test1;
    } else {
        trace (" Error loading variables ");
    }
}

//textbox.text = "hmm "+myVars.test2;

// if (myVars.getBytesLoaded < 1) { //failed to load; }

_________________________________
Leozack
Code:
MakeUniverse($infinity,1,42);
 
Oh and any thoughts on how to check for input from both query and post and choose either whichever is given or 1 or the other if they're both given? It's useful eg in php to be able to check the query and the post and take your pick

_________________________________
Leozack
Code:
MakeUniverse($infinity,1,42);
 
*sobs* I sware I'm loosing my mind here

The following correctly traces the variables from the text file

Code:
myVars = new LoadVars(); 
myVars.load("[URL unfurl="true"]http://localhost/test/test.txt");[/URL]

myVars._path = this   /* we are outside the onLoad method, to "this" is the current timeline */ 
myVars.onLoad = function (success) {
     trace(this._path) // will output "_level0" 
     if (success) {
        trace (" variables loaded "); 
        for( var prop in this ) {
            trace (" key " + prop + " = " + this[prop]); 
        }
    } else {
        trace (" Error loading variables "); 
    }
}

But right after that I put this and it is empty where the var should be

Code:
trace("The testtext var in the testfile is " + myVars.testtext);

Of course, the testfile.txt contains just

Code:
testtext=helloworld

_________________________________
Leozack
Code:
MakeUniverse($infinity,1,42);
 
If you do:

[tt]//
myVars = new LoadVars();
myVars.load("testfile.txt");
myVars.onLoad = function(success) {
if (success) {
trace(" variables loaded ");
for (var prop in this) {
trace(" key "+prop+" = "+this[prop]);
}
} else {
trace(" Error loading variables ");
}
};
trace("The testtext var in the testfile is "+myVars.testtext);
//[/tt]

- the trace will be "undefined", because when trace() is executed text file is not loaded yet.

Consider:

[tt]//
myVars = new LoadVars();
myVars.load("testfile.txt");
myVars.onLoad = function(success) {
if (success) {
trace(" variables loaded ");
for (var prop in this) {
trace(" key "+prop+" = "+this[prop]);
}
traceVar();
} else {
trace(" Error loading variables ");
}
};
//
function traceVar() {
trace("The testtext var in the testfile is "+myVars.testtext);
}
//[/tt]

Kenneth Kawamoto
 
So ... everything in the entire project has to go inside the onload? There's no way to just get the thing loaded into some global variables that everything else can access? I thought that's what the myVars object was for?

_________________________________
Leozack
Code:
MakeUniverse($infinity,1,42);
 
Yes but didn't I just load them in the lines above? How am I supposed to access them in the script if loading them above in the script doesn't count as loading them? Evidently I'm confused as to when it actually loads them. Of course in a preparse it won't be loaded but then that's always the case no?

_________________________________
Leozack
Code:
MakeUniverse($infinity,1,42);
 
To clarify - the way I understand it is if I say
Code:
myVars = new LoadVars();
myVars.load("testfile.txt");
Then I just loaded the vars into the myVars object and from then on I can say eg
Code:
trace("The testtext var in the testfile "+myVars.testtext);
I summise this because if I say
Code:
ThisIaAVar = "this is a nice var";
then that's it, it's set, and I can use it at any point below no?

so yeah, evidently I'm confused, please shine the light on the rather obvious thing I've missed and make me feel stupid ;P

_________________________________
Leozack
Code:
MakeUniverse($infinity,1,42);
 
Every data loading functions in Flash are asynchronous. (Except XMLSocket.send(), according to the doc.) It means the code execution does not wait for the data being loaded before moving on to the next line of code.

If you set a variable in the script you can access it straight away.

[tt]//
1. ThisIsAVar = "this is a nice var";
2. trace(ThisIsAvar);
//[/tt]

Because the script runs in sequence - line 1 and then line 2.

This one is different, because LoadVars.load() is asynchronous:

[tt]//
1. myVars = new LoadVars();
2. myVars.load("testfile.txt");
3. myVars.onLoad(){...}
4. trace(myVars.someVar)
//[/tt]

The code executes in this order: 1, 2, 4. It doesn't wait for the data being loaded before moving on to 4. Then when the data is loaded into myVars, finally 3.

This is why you get "undefined" for the line 4 code: trace(myVars.someVar).

Kenneth Kawamoto
 
I don't get undefined I just get blank it seems.

Ok so - how do I load variables into the script then, and actually wait for them to be done before carrying on? (why would I want to carry on before I have what I'm loading). I mean I could have the onload set a "loaded" variable to true and the line below somehow say wait for it to be true I guess

And on the other note, is there a simple way to do the query/post chec comparison? noone's mentioned it so I'm guessing I'll have to work something up

_________________________________
Leozack
Code:
MakeUniverse($infinity,1,42);
 
> I don't get undefined I just get blank it seems.

May be different version of Flash. Are you on Flash 8?

> Ok so - how do I load variables into the script then, and actually wait for them to be done before carrying on?

You are dealing with one simple text file, but for example in the real professional applications you will need to deal with multiple XML data from web services, and the data can be megabytes. You just can't expect them to get loaded and parsed instantly.

What I usually do is in the frame 1 I load and parse all the external data, while the Stage is showing "loading data..." message. When everything is loaded I move on to the frame 2, where all the actions are happening.

By the way if you want to just load few variables you don't have to use LoadVars at all. Use FlashVars instead - they load as soon as the Flash object is created, you can access them from the line 1, frame 1.

Kenneth Kawamoto
 
I'm using Flash MX (6)

I'm aware that when loading lots of data you have to wait from it. But I figured reading in a filename and/or path from a query or flashvars and then going to that place and loading in the contents of the file (a config file) giving the program the text and images to run, was the best way to make a .swf that doesn't need to be changed. You would just pass it where to look for a config file and it would load the config file and somehow work from there. What I'm working for is an interactive image archive basically where images link to eachother. And the config file would have to give the images to load aswell as specify the interactivity points and which image they goto. I can picture it all in my head fine for other languages or even using ini files but I've just never done something like this live with flash :/

And I just tried having a while loop under the onload doing nothing or counting until the vars were loaded and then carrying on, but it just locked it up instead. So I guess there is no way to stop the script and wait. Unless I can do that just by moving to frame 2 which won't be activated until frame 1 has loaded etc

_________________________________
Leozack
Code:
MakeUniverse($infinity,1,42);
 
You can do like this:

[tt]// frame 1
myVars = new LoadVars();
myVars.load("config.txt");
myVars.onLoad = function(success) {
if (success) {
doSometing()
} else {
trace(" Error loading config.txt ");
}
};
//
function doSomething() {
//Do something
}
//
stop();
//[/tt]

function doSomething() will not be executed until myVars is loaded with data.

Kenneth Kawamoto
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top