PDA

View Full Version : Absolutely failing to reference sharedObjects with two SWF files.


charlesshoults
07-24-2009, 02:43 AM
I'm putting together a small site monitoring tool for AIR. It consists of four swf files and all communication is routed out through the primary file. (It's derived from a component I have on my website). In two of the swf's, I have defined this:
import flash.net.SharedObject;
public var so:SharedObject;

In the main file, I sets up some initial elements:
so = SharedObject.getLocal("battletechlive");
if(so.data.connectedUsername==undefined) { so.data.connectedUsername="guest user" }
if(so.data.connectedPassword==undefined) { so.data.connectedPassword="xxxxxxxx" }
if(so.data.userID==undefined) { so.data.userID=33 }
if(so.data.loggedInUser==undefined) { so.data.loggedInUser="guest user" }
if(so.data.admin==undefined) { so.data.admin=0 }
if(so.data.rememberLogin==undefined) { so.data.rememberLogin="no" }
if(so.data.autoLogin==undefined) { so.data.autoLogin="no" }
so.flush();
From what I understand, so.flush(), writes the variables to disk. I've also seen a post where the user put so.close() at the end. I've tried both ways. In the receiving swf, I perform the following on a tick interval of 60 seconds:
so = SharedObject.getLocal("battletechlive");
soDetail.text = so.size + " bytes";
mx.controls.Alert.show(String(so.size));
so.close();

For whatever reason, I always get 0. If I read so.size from the swf which pushed the data, I get 174. Help, please?

charlesshoults
07-24-2009, 02:49 AM
Why is it I always find the answer just minutes after I give up and post the question?

See normally to read/create a SharedObject you write code like this:

var so:SharedObject = SharedObject.getLocal( "mySO" );

The above line of code will read the SharedObject called mySO or create one if it does not already exist in a similar folder structure as the one that follows (varies based on OS - I am using Vista):

C:\Users\Jimmy\AppData\Roaming\Macromedia\Flash Player\#SharedObjects\blabla\localhost\SWFA\mySO.s ol

Since the SharedObject is created in a folder specific to SWF A (see the "\localhost\SWFA" above), SWF B, will not be able to read this SharedObject, thus the both applications cannot share data between eachother.

In order to solve this, you must specify an additional parameter to the getLocal() method call to tell it to save the SharedObject in a folder location accessible by both applications. An example is:

var so:SharedObject = SharedObject.getLocal( "mySO", "/" );

What this will do now is create the SharedObject in the following folder structure:

C:\Users\Jimmy\AppData\Roaming\Macromedia\Flash Player\#SharedObjects\blabla\localhost\mySO.sol