User Guide

Storing local data 391
New shared object instances can be created using the static SharedObject.getLocal() or
SharedObject.getRemote() methods. The getLocal() method attempts to load a locally
persistent shared object that is available only to the current client, whereas the
getRemote()
method attempts to load a remote shared object that can be shared across multiple clients by
means of a server, such as Flash Media Server. If the local or remote shared object do not exist,
the
getLocal() and getRemote() methods will create a new SharedObject instance.
The following code attempts to load a local shared object named
test. If this shared object
doesnt exist, a new shared object with this name will be created.
var so:SharedObject = SharedObject.getLocal("test");
trace("SharedObject is " + so.size + " bytes");
If a shared object named test cannot be found, a new one is created with a size of 0 bytes. If
the shared object previously existed, its current size (in bytes) is returned.
You can store data in a shared object by assigning values to the data object, as seen in the
following example:
var so:SharedObject = SharedObject.getLocal("test");
so.data.now = new Date().time;
trace(so.data.now);
trace("SharedObject is " + so.size + " bytes");
If there is already a shared object with the name test and the parameter now, the existing
value is overwritten. You can use the
SharedObject.size property to determine if a shared
object already exists, as the following example shows:
var so:SharedObject = SharedObject.getLocal("test");
if (so.size == 0)
{
// Shared object doesn't exist.
trace("created...");
so.data.now = new Date().time;
}
trace(so.data.now);
trace("SharedObject is " + so.size + " bytes");
The previous code uses the size parameter to determine if the shared object instance with the
specified name already exists. If you test the following code, you’ll notice that each time you
run the code, the shared object is recreated. In order for a shared object to be saved to the
user’s hard drive, you must explicitly call the
SharedObject.flush() method, as the
following example shows:
var so:SharedObject = SharedObject.getLocal("test");
if (so.size == 0)
{
// Shared object doesn't exist.
trace("created...");
so.data.now = new Date().time;