Savedata not working... (AS3.0, Flex)

15 posts

Flag Post

I get a TypeError: Error #1034: Type Coercion failed: cannot convert Object@-495efc2f to Game. when trying to load “data.game” out of a SOL

 
Flag Post

You can’t save a custom class you’ve made yourself. (or at least, not as far as I know)
Try to convert it to a build-in class, like an Object or an Array.

 
Flag Post

This could get complicated…

 
Flag Post

You can…it just takes a little work. Understand, I have only played with this a little, so this post is about the limit of how far I can help you.

If you want to save non-public properties, have your class implement IExternizable. Basically, you must write functions to save/restore the object state, given an output/input stream, which supports writing/reading basic types like byte, int, float, string, etc.

In order to get Flash Player to recognize your class you have to register it using registerClassAlias before you save or load.

EDIT: This worked fine:

	public class ShootingRange extends Sprite {
		private var local:SharedObject;
		....
			registerClassAlias("example.shootingRange.Target", Target);
			local = SharedObject.getLocal("ShootingRange");
			targets = local.data.targets || [new Target(), new Target(), new Target()];
		....
		private function autoSave(event:TimerEvent):void {	
			local.data.targets = targets;
			local.flush();
		}
	public class Target extends Sprite implements IExternalizable {
		....
		public function readExternal(input:IDataInput):void {
			for (var i:uint = input.readInt(); i > 0; --i)
				newBulletHole(input.readInt(), input.readInt());
		}
		public function writeExternal(output:IDataOutput):void {
			output.writeInt(numChildren);
			for (var i:int = 0; i < numChildren; i++) {
				var hole:DisplayObject = getChildAt(i);
				output.writeInt(int(hole.x));
				output.writeInt(int(hole.y));
			}
		}
	}
 
Flag Post

Interface method readExternal in namespace flash.utils:IExternalizable not implemented by class Game.

public class Game extends Sprite implements IExternalizable

And what is an interface anyway?

 
Flag Post

An interface is kind of like a parent class, with a some differences:
1) You can’t have objects that are of the interfaces exact type, only inheriting class types.
2) An interface doesn’t implement any of its functions…that is left to inheriting classes.
3) An inheriting class must implement all functions that it inherits from an interface.
3) Classes use the implements keyword rather than extends to inherit from an interface.
4) Interfaces can only inherit from other interfaces.
5) Classes (or interfaces) can inherit from any number of interfaces.

The idea is that you might want to apply some algorithm (such as serialization) to objects of classes with totally incompatible class inheritance (like subclasses of Array and Sprite). By having both classes implement an interface, you assure the the prerequisites of polymorphism: each class implements the inherited functions, a single variable can hold any inheriting type, and a call to an inherited function will call the correct implementation.

Re: your error, make sure you have a function public function readExternal(input:IDataInput):void. Remember, you must implement every function in the interface.

 
Flag Post
ArgumentError: Error #2004: One of the parameters is invalid.
	at flash.net::SharedObject$/getLocal()
 
Flag Post

Can you post the code where you called getLocal? ArgumentError implies that there is an problem with an argument like invalid characters, rather than a deeper error, like the sol file or serialization.

 
Flag Post
public function onAddStage(event:Event):void {
			//Remove the event listener to get around a bug
			removeEventListener(Event.ADDED_TO_STAGE, onAddStage);
			
			registerClassAlias("game", Game);
			//Load the SOL
			saveData = SharedObject.getLocal("SW_DunRPG");
		}
 
Flag Post

Okay, it turns out that the objects inside SharedObject.data are restored during the call to SharedObject.getLocal (which in retrospect should have been obvious, but I thought it might be using a proxy). The error is probably being thrown somewhere is your readExternal, which means the bug is there or in writeExternal.

 
Flag Post

After doing NOTHING, I get this error:

ArgumentError: Error #1063: Argument count mismatch on Game(). Expected 1, got 0.

(Game needs a parameter that links it to the main class)

 
Flag Post

You might need to rethink how you get that reference. You can’t store/load it in the SharedObject since your instance of main will be different every time.

Might be easier to just have your instance of Main set a static reference to itself somewhere, and let other things like Game fetch that reference for themselves instead of passing it in.

 
Flag Post

Hmm… Still getting errors, will custom objects inside game be saved aswell?

 
Flag Post

Hmm… Still getting errors, will custom objects inside game be saved aswell?

For object to be serialized with a containing object, they must:
1) have their class registered with registerClassAlias,
2) implement IExternalizable if retaining nonpublic data,
3) and be sent to output (using output.writeObject(someObject) if the container implements IExternalizable).

The other option is to have the containing object serialize just the relevant information and recreate them, as I did in my first post.

 
Flag Post

Hm… I guess I’ll have to just convert everything into primitive vars and such…