Shared SharedObject in Flash

12 May 2007

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;

[as] var my_so:SharedObject; my_so = SharedObject.getLocal(“com.simonheys.screensavr”); [/as]

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;

[as] var my_so:SharedObject; my_so = SharedObject.getLocal(“com.simonheys.screensavr”,”/”); [/as]

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.

[as] class com.simonheys.projects.screensavr.ScreensavrPreferences { public static var PHOTOSTREAM:Number = 0, PHOTOSET:Number = 1, GROUP:Number = 2, FAVOURITES:Number = 3; public var useHighQuality:Boolean, username:String, sourceType:Number, sourceId:String, speed:Number; private var _soId:String; public function setSharedObjectId(soId:String):Void { _soId = soId; } public function storeInSharedObject():Void { var my_so:SharedObject = SharedObject.getLocal(_soId,”/”); = username; = sourceType; = sourceId; = speed; = useHighQuality; my_so.flush(); } public function retreiveFromSharedObject():Void { var my_so:SharedObject = SharedObject.getLocal(_soId,”/”); username =; sourceType =; sourceId =; speed =; useHighQuality =; } } [/as]

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:

[as] _preferences = new ScreensavrPreferences(); _preferences.setSharedObjectId(Constants.SHARED_OBJECT_ID); _preferences.retreiveFromSharedObject(); // retrieve a value highQualityCheckBox.selected = _preferences.useHighQuality; // store a value _preferences.sourceType = ScreensavrPreferences.PHOTOSTREAM; _preferences.storeInSharedObject(); [/as]

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


2 comments so far
Leave a comment →

As opposed to an Un-shared shared object!

Thanks for this info mate, will use!

13 May 2007


Yeah mate. So will I!

4 June 2007


Leave a comment

(will not be published) (required)