-
Senior Member
[AS2] Custom Class in a Shared Object
Does anyone know how I can save a custom class in a SharedObject and have its methods survive when the SWF is closed and reopened?
for example:
Code:
var myClass:CClass = new CClass("Value");
//class has a getValue method that returns the value
var my_so:SharedObject = SharedObject.getLocal("test");
if(!my_so.data.classes){
var myArray:Array = new Array(myClass);
my_so.data.classes = myArray;
my_so.flush();
}
trace(my_so.data.classes[0].getValue());
//returns "Value" when before swf closed
//returns null after close and reopen
Thanks.
_t
-
Senior Member
-
I don't think SharedObject can store object definitions with AS 2.0. However, I don't see any need to either...
What you need is a pair of functions to serialise / deserialise your object. EG:
Code:
class CClass {
private var p1:Number;
private var p2:String;
private var p3:Array;
private var a1:Number;
private var a2:String;
private var a3:Array;
function CClass (init:Object) {
if(init != undefined) fromObject(init);
}
public function fromObject(o:Object) {
for (var i in o) {
switch(i) {
case 'a1' :
case 'p1' :
this[i] = String(o[i]);
break;
case 'a2' :
case 'p2' :
this[i] = Number(o[i]);
break;
case 'a3' :
case 'p3' :
// Casting arrays is erratic as Array class sometimes
// treats arguments as instantiation values
this[i] = new Array().concat(o[i]);
break;
}
}
}
public function toObject():Object {
var o:Object = new Object();
for(var i in this) {
o[i] = this[i];
}
return o;
}
}
Then you store the toObject() output in SharedObject and can regenerate the class using either new CClass(data); or instantiate a new CClass() and call .fromObject(data); on it.
-
Senior Member
-
Senior Member
Hi AlsoGaiusCoffey,
I'm trying to understand what is happening here a bit more clearly, but having some trouble. Coudl you please explain what is going on in this function, specifically with the a1, a2, a3, p1, p2, p3.
Thank you for any help you can provide.
_t
-
Hi,
Sorry about that, I was just putting in some examples of initialising variables!
a1, a2, a3 and p1, p2, p3 are just variables of different types. Replace them with the variables you need to make your class run.
To summarise:
toObject() takes all of the variables that your object needs to run properly and assigns them as properties of an object that SharedObject can store.
fromObject() reinitialises your object with the same variables, taking care to make sure that the variables are cast to the correct type. (EG: "1"+11 is not the same as 1+11).
Or in other words, if your object is dependant on variables defined as:
var bob:String;
var canwefixit:String;
Then toObject would set the variables:
var o:Object = new Object();
o.bob = bob;
o.canwefixit = canwefixit;
And fromObject would reset them as:
bob = String(o.bob);
canwefixit = String(o.canwefixit);
No need to do it using switch, you can write out all the settings long-hand if you have the time.
G
-
Senior Member
Thanks. That makes sense.
_t
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|