If I load a variable from a text file into _root. How do I access it in actionscript for something like an if statement? I tried putting just the variable and _root.variable but neither worked. Also any dynamic text boxes I have correctly display the variable. It must be possible to use these external values for stuff other than text boxes right? The basic idea is to use these values in an if statement that will decide whether it should be in frame 1, 2 or 3.
Any help is appreciated.
u should load ur variable into level0 if u want to use it for _root, the external variable should work anyway!
yours,
h88
remember too that the variable will take time to load. It’s a good practice to create a preloader which looks for a particular variable from the txt file, like “flag=true” for instance. Located at the end of the txt doc, you just make sure to wait until flag=true in the Flash production before attempting to read the other variables in the txt file.
Alright I’m currently loading the variables into level0. When I test the movie and list the variables it tell me the 12 or so variables and the correct value they are equal to. But when I attempt to use them in an if statement it’s like it doesnt exist. I’ve tried all kinds of variations on the if statement.
if (outcome=win)
if (_root.outcome=win)
if (level0.outcome=win)
if (_level0.outcome=win)
and each of those with “win” in quotes.
When I list the variables when testing them they are given as _level0.variable = "whatever
".
And this is how the loading of the variables is written:
loadVariablesNum (“admin/match.txt”, 0);
I’m not a completely newbie at a/s but there are likely dozens of little tricks I don’t know. So chances are it’s something pretty simple…
easy mistake considering the nature of a/s is a little different than some of the more popular programing tools. I’ve seen this mistake a hundred times from newbies, including myself when I first started out. (ok I admit it… 95 of the 100 times were me. )
if (outcome=win)
if (_root.outcome=win)
if (level0.outcome=win)
if (_level0.outcome=win)
should be
if (outcome==win)
if (_root.outcome==win)
if (level0.outcome==win)
if (_level0.outcome==win)
if you use a single equals sign, Flash thinks that you are telling it to assign the strings to those variables, in A/S it is a double equals symbol which means “compare these objects”.
Omg!! I knew that too! I remember typing those double equal signs out before. lol. Guess that’s what happens when you go and decide to learn flash and end up taking a break from it… :
Thanks for the reminder
Alright no matter what I do flash doesn’t recongize that the variable exists when accessing it in a/s. It will display it in text boxes and it will list it in the output when you debug and list the variables.
debug ex:
Variable _level1.status = “Defending”
Now if I place a textbox and give it that variable name it will display “Defending”. But if I trace the variable or try to use it in a/s it is returned as undefined.
So either I need a real good explanation of why it isn’t working or maybe someone could make a real quick .fla demonstrating the loaded variable being used in some a/s. Thx again.
Was anyone able to figure this problem out? I’m having the same problem as Timeberboom and decided to search the forum before posting on it, but this thread looks unanswered.
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:
loadVariablesNum(“data.txt”,0);
trace("x = " + x);
- 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.
I was surprised this thread rose from the dead. Anyway I didn’t post any reply that I fixed it because I assumed the thread would never return so here ya go. As stated above (I think. I’m doing this from memory) you have do a check that the last variable in the data.txt file is loaded. So after you load the txt file with:
loadVariablesNum(“data.txt”,0);
In the next frame make an if statement to check it.
if (y==23) {goto this and that and make some a/s that uses the vars}
else {goto the next frame which in turn returns you to this one for the test again}
Which makes me wonder, can you just point a piece of a/s to goto the same frame it’s in and execute the script from the beginning again?
Also I made a little timer that counts to some number (I think I gave it around 15-20 sec) and if the data isn’t loaded yet because it timed out it will restart the loading process by returning to frame 1, reseting the timer and loading data.txt again.
Hope this helped I really cut some corners in what I wrote above. If anything is unclear feel free to reply.