Shared SharedObject in Flash

One of the problems I had when writing Flash Screensavr was sharing preferences between two separate and different SWF files - the Preferences panel and the Screensaver itself. Obviously a job for SharedObject.

However there is a trick to creating a SharedObject that behaves how you'd expect.

If you're like me, you would think the correct code is;

Actionscript:
  1. var my_so:SharedObject;
  2. my_so = SharedObject.getLocal("com.simonheys.screensavr");

But this results in a SharedObject which can only be accessed by the SWF that created it...not what I wanted. The not-very-obvious solution is to add a second parameter which is referred to simply as 'localPath' in the docs;

Actionscript:
  1. var my_so:SharedObject;
  2. my_so = SharedObject.getLocal("com.simonheys.screensavr","/");

That's it! You now have a useful SharedObject which you can access from any SWF on your machine. Having spent literally hours trying to track down the solution I decided to write a nice preferences class in a similar style to the one I use in Cocoa. Applications usually have unique preferences, so this is customised for each one.

Actionscript:
  1. class com.simonheys.projects.screensavr.ScreensavrPreferences
  2. {
  3.     public static var
  4.         PHOTOSTREAM:Number = 0,
  5.         PHOTOSET:Number = 1,
  6.         GROUP:Number = 2,
  7.         FAVOURITES:Number = 3;
  8.        
  9.     public var
  10.         useHighQuality:Boolean,
  11.         username:String,
  12.         sourceType:Number,
  13.         sourceId:String,
  14.         speed:Number;
  15.        
  16.     private var
  17.         _soId:String;
  18.        
  19.     public function setSharedObjectId(soId:String):Void
  20.     {
  21.         _soId = soId;
  22.     }
  23.    
  24.     public function storeInSharedObject():Void
  25.     {
  26.         var my_so:SharedObject = SharedObject.getLocal(_soId,"/");
  27.         my_so.data.username = username;
  28.         my_so.data.sourceType = sourceType;
  29.         my_so.data.sourceId = sourceId;
  30.         my_so.data.speed = speed;
  31.         my_so.data.useHighQuality = useHighQuality;
  32.         my_so.flush();
  33.     }
  34.  
  35.     public function retreiveFromSharedObject():Void
  36.     {
  37.         var my_so:SharedObject = SharedObject.getLocal(_soId,"/");
  38.         username = my_so.data.username;
  39.         sourceType = my_so.data.sourceType;
  40.         sourceId = my_so.data.sourceId;
  41.         speed = my_so.data.speed;      
  42.         useHighQuality = my_so.data.useHighQuality
  43.     }
  44. }

I did think about having methods called 'save' and 'load' but was obviously feeling verbose that day. You could of course have this as a Singleton in Flash, but I didn't need to.

The code for using the prefs is then as simple as this:

Actionscript:
  1. _preferences = new ScreensavrPreferences();
  2. _preferences.setSharedObjectId(Constants.SHARED_OBJECT_ID);
  3. _preferences.retreiveFromSharedObject();
  4.  
  5. // retrieve a value
  6. highQualityCheckBox.selected = _preferences.useHighQuality;
  7.  
  8. // store a value
  9. _preferences.sourceType = ScreensavrPreferences.PHOTOSTREAM;
  10. _preferences.storeInSharedObject();

Where Constants.SHARED_OBJECT_ID is something like com.simonheys.preferences.screensavr.


12 May 2007 in ActionScript, Source Code

2 Comments

    • 1
    • Comment by gabes
    • 13 May, 2007 at 8:30 pm

    As opposed to an Un-shared shared object!

    Thanks for this info mate, will use!

    • 2
    • Comment by MsBrittle
    • 4 June, 2007 at 1:18 pm

    Yeah mate. So will I!


Leave a Comment


(required)
(will not be published) (required)

Follow comments through the RSS 2.0 feed. Trackback from your own site.