Help with saving a class to a SharedObject

5 posts

Flag Post

Alright, let me preface this with “I have never worked with Singletons before”. Though, that doesn’t mean I haven’t at least tried to work this out myself before coming here. I’m not looking for my hand to be held, but I would still appreciate guidance and help.

Also, I’m a derp. So my code is going to be derpy as best. (I’m self-conscious about my code when showing other people, so be gentle)

What I’m trying to do, is take a class, and save that class to a SharedObject, that way I can easily save and access a great deal of data, and different sets of data, without having to save each individual piece of data I want to the SharedObject.

Now, here is my function for creating/loading the save.

public static function initiateSaves():void
		{
			registerClassAlias("SaveData", SaveData);
			save = SharedObject.getLocal("Save");
			if (save.data.created == undefined)
			{
				trace("No save data available, creating SharedObject");
				save.data.created = true;
				SaveData.getInstance();
				SaveData.test = "Test";
				save.data.testSave = SaveData.instance;
				save.flush();
			}
			else
			{
				trace("Game SharedObject loaded");
				SaveData.instance = (save.data.testSave as SaveData);
				trace(SaveData.test);
			}
		}

As far as I can tell, it “works” when it comes to saving the instance. However, when I try to trace a piece of data from it, I get nothing.

Here is my SaveData class

package
{
	import flash.geom.Point;
	import flash.net.SharedObject;
	
	public class SaveData
	{
		private static var _instance:SaveData = null;
		public static var instance:SaveData;
		public static var test:String;
		
		public function SaveData()
		{
		
		}
		
		public static function getInstance():SaveData
		{
			if (_instance == null)
			{
				_instance = new SaveData();
				instance = _instance;
			}
			else
				throw new Error("Instance already exists");
			
			return _instance;
		}
	}
}
 
Flag Post

Your problem is the Singleton, it’s a) not well written and b) used in the wrong way.
It looks like you just copy&pasted the singleton code partly without knowing what it does or why you would want to use it.

The point of a singleton is to guarantee that only one instance of a class can be generated.
Instead of creating the class with the new operator, your use the static method getInstance()
You should create a variable referencing the object as you do with any other class.

And that’s it. The idea is not to make all members of that class static. (as you do with test)
The way you throw the error doesn’t make much sense. It’s fine to query the class for the single instance multiple times – that’s actually how you guarantee that instance to be one and the same even when called multiple times.

Don’t force patterns.
Create the class you want to hold the data as a regular one, if necessary, turn it into a singleton later.

 
Flag Post

I can assure you I didn’t copy&paste, the rest is true though, I have no idea what I’m doing with a Singleton. I’m going to try a different method first, something I’ve done before. Serializing the entire class using a ByteArray.

 
Flag Post

Done! Alright, I’m happy with what I have now.

public static function initiateSaves():void
		{
			registerClassAlias("SaveData", SaveData);
			saveObject = SharedObject.getLocal("Save");
			var saveByte:ByteArray;
			
			if (saveObject.data.testSave != null)
			{
				trace("Game SharedObject loaded");
				saveByte = saveObject.data.testSave as ByteArray;
				trace(saveByte);
				save = saveByte.readObject() as SaveData
				trace(save.test);
			}
			else
			{
				trace("No save data available, creating SharedObject");
				
				save = new SaveData();
				save.test = "Test";
				
				saveByte = new ByteArray();
				saveByte.writeObject(save);
				saveObject.data.testSave = saveByte;
				saveObject.flush();
			}
		}
 
Flag Post

I don’t think you need a singleton here, no.

The byte array trick works as long as (i) you have only simple types in your data object (no object references and really definitely no references to the display list), and (ii) Adobe don’t change their data format.

Personally I’m a bit cagey about (ii) so I write everything into my SOs as arrays or keyed objects (i.e. Object, not a specific class).