Use SharedObject to save data on users computer

By using SharedObject you are able to store limited amounts of data on a user's computer. It's works in the same kind of way that your browser stores cookies. However there are some limitations with using this. Sometimes swf files may not be able to write the data, and the data can sometimes be deleted without your knowledge. Users can set their own limits on how much space flash player can use on their computer. When they lower the amount of disk space available, some local shared objects might get removed.

I'll show you a basic example of using SharedObject. We will have an input text field on stage and a button that will save the text in the text field.

Create a new AS3 Document and put a text field on stage and give it an instance name. I will call mine theText_txt.
Next make a button on stage and give it an instance name. I called mine save_btn.

Create a new layer in the main timeline and call it actions and lock it. Open up the actions panel and enter the following:

  1. var mySO:SharedObject;
  2. mySO = SharedObject.getLocal("myfirstSO");

The first line creates the shared object. The second line gets a local shared object called myfirstSO. Nothing is stored in it at the moment but we will assign data to it shortly.

Now when we click the button we want to assign whatever text is in the text field to the shared object. In the actions panel enter:

  1. save_btn.addEventListener(MouseEvent.CLICK, saveText);
  2.  
  3. function saveText(e:MouseEvent):void
  4. {
  5. mySO.data.myText = theText_txt.text;
  6. }

This has now assigned the text in the text field to a variable called myText in the SharedObject.

OK now that the text is saved we will want to populate the text field with that saved data when you open the swf again. In the actions panel enter the following.

  1. if (mySO.data.myText)
  2. {
  3. theText_txt.text = mySO.data.myText;
  4. }

This checks to see if there is any data in the myText variable in the SharedObject. If there is then it assigns that value to the text field.

Now test the movie. Enter some text in the text field and click your save button. Close the swf and test the movie again. You will see that the text you entered before is in the text field.

You can download the example here: SharedObject.zip

Hope this was helpful,
Ed

3 Responses to “Use SharedObject to save data on users computer”

  1. Stefan Wessels Says:

    Hey Ed!

    You can also use: mySO.flush() to force Flash to save the storedObject immediately – I know on FL I have to use this method to make sure data is saved – probably works better on desktop version of Flash.

    “Immediately writes a locally persistent shared object to a local file. If you don’t use this method, Flash writes the shared object to a file when the shared object session ends–that is, when the SWF file is closed, that is when the shared object is garbage-collected because it no longer has any references to it or you call SharedObject.clear().”
    (http://help.adobe.com/en_US/AS2LCR/Flash_10.0/help.html?content=00001504.html)

  2. Ed Says:

    Hey Stefan,
    Yeah I knew about that. We have used that in Structure Creator. I just haven’t updated my site in quite a while to mention it. Thanks for the link.

  3. adam Says:

    Thanks man …u solved my problem……keeep posting this kind of stuffs….

Leave a Reply