AS3: Quickhint: Shared Objects

Last week while working on the interface for my new upcoming library I needed a way to save some data on the users computer, altough I’m working with AIR I could easily save an xml or a txt file to the users local computer, but as soon as I would start writing a pure as3 project I would have to rewrite it again, as I can’t be sure that the client will have Flash Player 10 or even the rights to save to it’s local harddisk, some people working at bigger companies are not allowed to save anything to their local harddisk due to security rights.

The solution for this is simple yet effective, you need to use SharedObjects aka. Cookies in Flash.

Shared Objects can do the following:

  • Maintain Local Persistence: Saves data locally on the users drive, for example you could save the last application state or the score of the last game, create a local highscore list and many many more applications for local use.
  • Store and Share Data on FMS: Basically the same as before, but with the difference that you saving the shared object on a flash media server. Good thing about it, it can be shared throughout multiple computers, the same user using the same application on multiple computers can save its data on a server.
  • Share data in realtime: using FMS you can aswell for example save the state of a multiuser application saving the current state of the applcation, every new user will get the current state based on the shared object

More information about Shared Objects can be found HERE

Let me show you a few lines of code and explain what you can do with it.

The most basic example is how to write data into a shared object.

var _so:SharedObject;
_so = SharedObject.getLocal("myLocalData");
_so.data.firstname = "myfirstname";
_so.data.lastname = "mylastname";
_so.data.age = "30";
_so.flush();

In the example above we create a new variable called _so of the type SharedObject, after that we use the getLocal() method to create the shared object, if no shared object does not already exist it will be created. if the call fails or the parameters passed along are invalid, flash throws an exception. Calling the flush() method the data will be saved to the users local harddisk.

Next up lets see how you can read from a persistent shared object.

var _so:SharedObject;
_so = SharedObject.getLocal("myLocalData");
trace( _so.data.firstname); //traces myfirstname
trace(_so.data.lastname); // traces mylastname
trace(_so.data.age); // traces 30

This time we do basically the same as before, but instead of saving data we call data, when calling _so.data.age you are going to grab the sharedobjects property named age which results in 30.

If you rather prefer to save SharedObjects to an FMS, you can use it this way:

var _nc:NetConnection;
var _so:SharedObject;
 
_nc = new NetConnection();
_nc.connect("rtmp://yourFMSserver/yourAppName");
_so = SharedObject.getRemote("myRemoteData", nc.uri, false);
_so.connect( nc );
_so.setProperty( "names", {firstame:myfirstname, lastname:myLastname} );
_so.setProperty( "age", {"30"} );

This time we create a new NetConnection to the Flash Media Server and it’s application, calling the getRemote() method acts the same as using the getLocal() method, but you also need to pass the netconnections URI to be able to save the the sharedobject remotely.
Differently from the local version we use the setProperty() method to save data on the remote Shared Object.

These are the basics for handling Shared Objects, if you have any further questions just drop me a line and I might be able to help you out.

Creative Commons License
The AS3: Quickhint: Shared Objects by Tiago's Weblog, unless otherwise expressly stated, is licensed under a Creative Commons Attribution 2.5 Switzerland License.
Leave a comment

7 Comments.

  1. Hey Tiago!

    I’m in a bit of a pickle with remote shared objects. I’d like to discuss this with you. My skype is jeff.boshers.

  2. Hi Jeff, what is your question exactly?
    I’ll be on the road the next few days, so it makes more sense to communicate through email.
    Cheers
    Tiago

  3. Hi Jeff, Thanks for this posting it helped. I am trying to build a whiteboard and I am trying to pass up MovieClips or Sprites through SharedObjects. Not having any luck. I follow the same example as in your example. When I trace at the client side it shows its a movieClip but once the SharedObject change is received by another user it shows as ‘undefined’.

  4. Tiago,

    Basically I am making a video chat with text chat capabilities. I am having a hard time making the user’s name appear under their video.

    the code I am using is “inherited” from a whack programmer :sad: .

    I would like to email you the .as file and we could laugh at it together.

  5. Jeff, send me an email to tiago . dias [ at ] six4rty . ch

  6. From everything I can find you can’t share a movieClip, so instead I was successful in sending up an XML object that recreates the objects. Works great. :razz:

Leave a Reply

Your email address will not be published.


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Trackbacks and Pingbacks: