
Page 2 of 3
Practical Application of Loaded Variables:
The simplest and probably most common use for loadVariables is to update text fields on the stage. As in the earlier example, if you create a site with one area of text which requires regular updates or changes, loadVariables is ideal. Simply create a dynamic text field on the stage where you want your values to be displayed and set its variable name to ‘news' or something appropriate. Next create your text file (‘sitenews.txt' for example) and throw in an update:
&news=Today nothing much happened.
Save your text file and add the following action to your movie:
_root.loadVariables("sitenews.txt");
This should make your text appear within the text field in your SWF. Hey presto! The above code treats _root as the target movieclip for our variables (this is perfectly legal). Also note that if your text doesn't look as pretty as it might had you typed it manually within Flash that's because your font isn't embedded; embedding the font in the field should solve the problem.
The next most common use for loadVariables is probably to update data in a system which changes often, such as the price of a product in an online store. This is a good example because it leads into ‘swonking' and parsing.
Swonking
Flash MX users might decide to skip over this paragraph if they intend to use LoadVars, but it's probably worthwhile everyone knowing.
Definition: swonk, v. – to check repeatedly if a loadVariables operation is completed yet.
To the best of my knowledge it was our very own 20 Ton Squirrel who coined the term swonking. One of the most common questions about loadVariables on the forums is of the form "I'm loading variables using loadVariables, then performing some operation on them. All my code is correct but the values of my operations are always wrong". The reason for this is that loadVariables isn't an instantaneous thing. When you ask for Flash to load the file, it takes a while, depending on the size of the file and the user's connection. Thus the values aren't immediately accessible. If I have a file called "data.txt" containing a variable ‘users' with the value 24 and I execute the following code:
_root.loadVariables("data.txt");
trace(users);
The trace operation will show "0", not "24". The moral of the story is that you need to check to see if your variables are loaded before trying to use them. The process of doing this is commonly called ‘swonking' on this and other sites and generally involves the following steps.
First you add an additional variable to the absolute end of your file called EOF and give it the value ‘true' or something similar. So the contents of my data.txt from above would now be:
&users=24&EOF=true
Next you use two looping frames, a looping movieclip or a clipevent to check over and over to see if EOF is true yet. Once EOF is true, your other variables must all be loaded. It's that simple. Describing how to set it up might be a bit long winded, so just grab this source file and take a look.
Assuming we now know how to load our data in, and check it's fully loaded, we now need to give some thought to parsing.
Parsing
Parsing is a fancy word for "doing stuff to data". If you recall, above I began outlining how loadVariables could be used to update prices for products in an online store. The more studious of you will remember also however that I stated at the outset that all values loaded using loadVariables are Strings and Strings can cause problems when you try to use mathematical operations on them. The solution can be found in parsing your data appropriately.
Once you've loaded your data and swonked it to be sure it's all ready to be used, you can perform operations on the variables to convert them (if needed) into other formats. For instance if my text file contains a definition:
&userCount=24
I would use the following code in Flash to convert that from a String to a proper Integer which I can add, divide, etc.:
userCount = Number(userCount);
Simple huh?
Another parsing example I mentioned above related to the issue of newlines. Recall that if you have carriage returns and newlines in your text file, they will be loaded into Flash as part of the variable they follow. While this isn't really desired, we also don't want our text file to be completely illegible, so you can create a function which goes through looking for newline characters at the end of a loaded String and strips them off.
Load Means Send
loadVariables is nifty for bringing in external data to Flash, but it is also commonly used to export variables for use elsewhere. When passed an additional argument describing which HTTP method to use to send variables, all variables on the current timeline will be sent by Flash to whatever file you specify in the file argument. By the ‘current timeline' I mean the timeline from which the code is executed.
This makes loadVariables perfect for developing interaction with databases as you can send variables to a backend script (PHP, ASP, etc.) which draws upon the variables passed to retrieve information from the database, or perhaps processes the variables passed, then passes them back. See my loadVariables Interaction tutorial for more on this topic.
