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:
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
Open a new Flash document in the same directory and, on the stage, create a dynamic textfield and assign its variable property to x.
On the main timeline, type the following a/s code:
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.
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:
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:
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:
Create a button in frame 1 and give it the instance name button1.
In the second frame, insert the following code on the timeline:
x = Number(x);
x = x +100;
trace("x = " + x);
stop();
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?
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? **