This post is an overview of the various Formats and Methods we have available to us when passing Data into and Out of Flash. And goes onto cover the evolution of LoadVariables to the MX Load Vars object and Flash Remoting.
The concept 'Format is everything' - will become a bit more clear on subsequent examples in the Technical reference area.
loadVariables - An older function, introduced in Flash 4, that we have all grown to love (or hate). It still remains useful in Flash MX, however the new loadVars Object is a more powerful alternative. LoadVariables sends and loads data via the 'POST' or 'GET' method as Name/Value pairs, which are formatted as follows: Name=Value&otherName=otherValue&myName=myValue... As you can see it's a straight forward, easy to use, and a powerful format but does lack some structure. (Most data on the internet is transferred via Name/Value Pairs).
XML Object - The XML Object and related methods in Flash have been available since Flash 5, but sometimes can take a bit longer to do all the XML parsing in Flash at first. It provides a much more structured format for reading and sending data - this is the real advantage. The flash XML Object use's the XML format (what else) for reading and sending data to and from flash. One large advantage the XML object has is that it relies less on middle-ware scripting. Flash applications based on XML have a bit easier time talking to other outside applications that may not be setup specifically for the Flash application.
Flash Remoting via AMF - With the release of MX Macromedia has introduced a new format and method for reading and sending data in Flash, 'Flash Remoting with AMF'. AMF (ActionScript Message Format) is a proprietary format so the actual format the data is sent in is not known. Flash Remoting is still in beta for most environments so it's a bit to early to tell how it will turn out. While it is a novel idea special server software and setup is needed to use Flash remoting, something that's not really possible unless you own your own production server and have root rights. The price of using Flash remoting is still to be set, who knows how much it will be. The Salsa (Java) versions of flash remoting look promising however they are still in beta.
Load Vars Object - The loadVars Object is one of the greatest additions to Flash MX. It is much like the flash XML Object with the exception that it utilizes a Name/Value Pairs format instead of an XML format. With loadVariables Name/Value pairs are read into flash and just float around the MC you loaded them into (which acts as an object as well), until you assign them to a text field or do something with them. With loadVars each Name/Value pair is assigned to an Object you specify, for example myData = new LoadVars(); myData.load("myData.php"); - each Name/Value pair loaded from this file is assigned to the myData object. Which can be accessed at anytime with myData.Name - that will give you a result of the Value specified by Name.
-------------------------------------------------------------------------------
Which Method do I use?:
Basically it's up to you. Their is no right or wrong method or format. Everything listed above does the basically the same thing. All have advantages and disadvantages, some are better for certain tasks then others, some are worse. When first starting out with loading and sending data to and from external sources I would recommend choosing one and becoming really familiar with how it works, then you can try some others out. After a while you will be able to determine the best method for the task without even thinking about it.
(The Load Vars Object is highly recomended however).
----------------------------------------------------------
Evolution of loadVariables to Load Vars
The best way to compare these two methods is to think of how and where they load Data into.
With the older loadVariables method - you specify the Movie Clip to load the variables into or out of. If no movie clip is specified they are loaded into the Movie clip that the request was sent from. This was the reason that we had to worry about specifing target paths such as (this, _parent, _root, etc) with the loadVariables function. It's easiest to think of each Movie Clip variables from loadVariables where loaded into As an Object (Movie Clips are objects anyways).
Now if you wanted to access a variable in a different Movie Clip from the one you where currently in - you could specify - _root.MyMovieClip.MyVariable;
With Load Vars - it's much the same, but is a bit more Object Oriented, more structured, then with loadVariables. The reason is that each time you load Variables using Load Vars - you create a New Object (Movie Clip). This is shown with something like:
myCarData = new LoadVars(); // Initialize data Object..
myCarData.onLoad = someFunctionOn; // function to invoke on load.
myCarData.load("getCarData.php");
This creates a New Object called myCarData. Any variables that are loaded in the Name/Value format (Name=Value&OtherName=OtherValue) - Can be accessed in that Object in much the same way that you where used to with Flash 5 - ie:
myCarData.Name; - would contain a value of 'Value'
myCarData.OtherName; - would contain a value of 'OtherValue'
In much the same way you can add new items to your load Vars object and send them - this would be equivalent of adding some new variables to a Movie clip in in Flash 5 - then sending only the variables located in that Movie clip out of Flash. Load Vars just makes this process more structured and Object oriented.
On a side not let's take a look at the LoadVars.onLoad method. This may seem like a new concept, but how different is this really from the onClipEvent(data) - function in Flash 5. If we think of Movie Clips as Objects - It becomes obvious that the .onLoad method is just an extension of the onClipEvent(data) event handler from Flash 5. (Just a lot easier to use)..
So you can see that not much has really changed. Well with the exception of the Event handlers such as myVars.load, myVars.onLoad, etc - which are very very similiar to their XML Object counterparts, and which make things a lot easier in the long run.
------------------------------------------------------
That's about it for now.
Comments? - what's your favorite method.