loadVariablesNum() problem

Hi, all. I’m basically “bumping” a dead thread that contained an issue that was never resolved. The old thread was called “variables” under the Flash 5 forum.

Here is a recap of the basic problem:

It seems that when the loadVariablesNum() command is used to read in variables from a textfile, the values of those variables can be displayed in dynamic textboxes, but you can’t operate on them with good ole’ fashioned ActionScript.

Here is a concrete example for anyone interested in solving the problem or explaining the answer:

  1. First, create a text file and place a variable or two inside using the MIME format: x=12&y=23

For the purposes of this post, let’s call the file data.txt

  1. Open a new Flash document in the same directory and, on the stage, create a dynamic textfield and assign its variable property to x.

  2. On the main timeline, type the following a/s code:

loadVariablesNum(“data.txt”,0);
trace("x = " + x);

  1. Save the file and run it. You should see the value 12 appear in the textbox (good news). However, the value 12 will not appear with the trace command (bad news). Specificying _root.x doesn’t seem to help either. It is as if the program does not recognize the variable.

Does anyone know the solution to this problem? I assume it is something simple, but, sigh, I’m not sure. Any help would be appreciated.

=)

Try the XML Object instead:

myXML = new XML();
myXML.onLoad = function(){
_root.x = this.x
}
myXML.load("data.txt");

Should work since your using flash 5 (i assume)!

Thanks, h88. Unfortunately, that doesn’t seem to work. I’m using Flash MX; I don’t know whether that matters or not.

Ok, I think I’ve finally figured this out. A bit of surfing led me to a nice thread on the bit-101 forums:

http://www.bit-101.com/forum/viewtopic.php?t=699

Apparently the problem stems from an asynchronicity between the rate at which the loading from the external file takes place and the rate at which the trace command (or any other code that operates on the loaded variables) is executed. The asynchronicity is not a problem for the dynamic text box.

One way to fix the problem is to loadVariables in frame 1 and then include the ActionScript that operates upon those variables in a second frame (or use preloader tricks to prevent the movie from advancing until everything is loaded).

Well, onLoad can do the job, i wanted to reply yesterday, but the forums were down, so i couldn’t submit my message, but here you go anyway if your still interested:

Flash MX?

Great, then use LoadVars.

Insert this script into your _root timeline:

myLoadVars = new LoadVars();
myLoadVars.onLoad = function(){
_root.myTextBox.text = this.x
}
myLoadVars.load(“data.txt”);

Data.txt Content is:
x=12&y=23

Now create a dynamic text field, and use the instance field (not variable) to call it myTextBox .

Press Ctrl + Enter and then you should get your content showing!

Well What basicly onLoad does is on load, you know, when the file get loaded, onLoad runs, this way you will solve your timing problem!

I don’t really recommend onEnterFrame.

Hi, h88. I appreciate your help in trying to solve this problem.

Unfortunately, the code you provided doesn’t solve the problem. Getting the data to display in a text box isn’t the problem. The problem involves being able to operate upon the data within the script. For example, if we modify the code you provided in the following manner (lines 6 and 7):

myLoadVars = new LoadVars();
myLoadVars.onLoad = function(){
_root.myTextBox.text = this.x
}
myLoadVars.load(“data.txt”);
x = x + 100;
trace("x = " + x);

The trace command will display the value of 100 (and not 112, as it should if it has added 12 + 100). The problem appears to be that the trace command is executed before the movie has a chance to load the information from the textfile (depite the fact that the text file is small).

To solve the problem, I think it is necessary to stall the ActionScript code until the variables have loaded. One simple way to do this (and not the best way) is to have one frame call the loadVariables command and then stop until a button in pressed. Once the button is pressed, the movie advances to the next frame where the relevant computations are executed. Here is a simple example, that builds on the code I posted previously:

  1. Assuming the text file already exists as data.txt, open a new document and, in the first frame, place the following code on the timeline:

loadVariablesNum(“data.txt”,0);
button1.onRelease = function(){
gotoAndPlay(2);
}
stop();

  1. Create a button in frame 1 and give it the instance name button1.

  2. In the second frame, insert the following code on the timeline:

x = Number(x);
x = x +100;
trace("x = " + x);
stop();

  1. Save the file and run it.

Now, by the time you’ve pressed the button (or the user has read the instructions presented in the first frame, if we insert some), the variables have fully loaded. After pressing the button, the A/S code is exectuted properly in frame 2. x now equals 12 + 100 or 112.

I’m sure there better and more clever ways of solving the problem. Any suggestions gang?

To solve the problem, I think it is necessary to stall the ActionScript code until the variables have loaded.

Did you try adding the trace command inside onLoad?

========Flash========

myLoadVars = new LoadVars();
myLoadVars.onLoad = function() {
x =Number(this.x)+100;
trace("x = "+x);
};
myLoadVars.load(“data.txt”);

=========Data.txt==========

&x=12

========Output Window=======

x = 112

I got it working using the way above

Yes, that works nicely. Does this imply that, if this approach is used, that all the program code that uses the data from the file will need to be contained within the onLoad braces?

*Originally posted by chris23 *
**Yes, that works nicely. Does this imply that, if this approach is used, that all the program code that uses the data from the file will need to be contained within the onLoad braces? **

Yes!

Thanks for your help, h88! It is nice to have a safe place to bouce ideas around with creative people.

C:-)

np :), am glad you got it working.