Tutorial details:
Written by: Jesse Stratford
Time: 40 minutes
Difficulty Level: Intermediate
Requirements: Flash MX
Topics Covered: How to use the new SharedObject feature of Flash MX for local data storage.
Assumed knowledge: Variables, Objects, Methods.

Another of the great new features of Flash MX is the SharedObject. A SharedObject in the most basic sense is a contraption designed to allow local storage of data pertinent to a user on that user's system. Like a cookie but better. If you can't think of any good reason to store data on a user's drive, you're not thinking hard enough. As an example, you could use a SharedObject (SO henceforth) to determine if a user has already visited your site - perhaps allowing them to automatically skip the intro animation after the first time. There are plenty of more complicated and cooler applications also.

The first thing to note about SOs in Flash is that they can be disabled on the client side. This will obviously thwart all our cool applications of them so straight off, here's some code to check if SOs are enabled on a user's system:

function checkSO() {
 // Does this client allow sharedobjects to be stored?
 // Create a dummy SO and try to store it
 mySO = SharedObject.getLocal("test");
 if (!mySO.flush(1)) {
  // SOs not allowed on this system!
  // Prompt user to change settings
  System.showSettings(1);
 } else {
  // SOs allowed
  trace("Your system allows sharedobjects");
 }
}
checkSO();

Don't fret, I've never been one to throw you in the deep end, but that code is important to begin with. Dump it on a keyframe in a new Flash MX movie and run it to be sure your system supports and allows SOs before we begin. (Also note that the above code only checks if your system supports SOs of 1 byte, so if you want to adapt it for any use other than checking whether the feature is on or off, keep that in mind.)

Let's get down to business with a whirlwind revision of what an Object is. An Object is a way to store (generally related) data in a nicely categorized fashion. So if we have 100 customers, instead of using:

Cust1Name = "Joe";
Cust1Age = 20;
Cust1Pet = "Dog";
Cust2Name = "Mary";
Cust2Age = 19;
Cust2Pet = "Dog";
...

We object oriented programmers use:

Cust1 = new Object();
Cust1.name = "Joe";
Cust1.age = 20;
Cust1.pet = "Dog";
...

To create a new SharedObject we use:

mySO = SharedObject.getLocal("test");

This is also the code we use to load up a previously saved SO. Here, "mySO" is the Object name we will refer to the SO by within Flash, while "test" is the file name it will be given on the system. SOs are stored locally with a ".sol" extension by default. Note that the file name field may contain forward slashes but most other special characters are not allowed, including ~ % & ; : " ' , < > ? # and spaces.

At any point after having created your SO, provided you have not deleted it, you can write it to the user's disk using the flush method, which returns true, false or "pending". A Boolean return value of true indicates that the write operation was approved and successful while false indicates the opposite. We will deal with a "pending" return value in a moment. The physical file on the disk is not created/altered until the flush method is invoked.

mySO = SharedObject.getLocal("test");
mySo.data.name = "Joes";
success = mySO.flush();

Continued overleaf...