KennyRules
KennyRules

Reputation: 221

How to properly save array of classes via StoredObject? (AS3)

in my current program, I have a class that acts as a container for relevant data. Let's say it's a Person class. Person has a private myAge:int attribute that gets set when the program runs. In addition, the Person is added to an array and saved. Then all of the people from the array are called and ages are traced. Here is the main code of interest:

// In the Document class. 
registerClassAlias("Person", Person); 
registerClassAlias("AddressBook", AddressBook);
myAddressBook = new AddressBook();
myAddressBook.addNewPerson(75);


// In the AddressBook class when a new AddressBook is created:
// I load the addressEntries, which is an array that will contain instances of Person class.

sharedObjectAddressEntries = SharedObject.getLocal("storedAddressEntries");

if(sharedObjectAddressEntries.data.storedAddressEntries != null)
{
    addressEntries = sharedObjectAddressEntries.data.storedAddressEntries;
}
    else
    {
        addressEntries = new Array();
    }

// This is what I do to add a Person to the array:
var aPersonEntry:Person = new Person();
aPersonEntry.addAge(anAge); // This just sets private field to anAge.
addressEntries.push(aPersonEntry);
saveStoredPeople();       

//This is how I save them:
sharedObjectAddressEntries.data.storedAddressEntries = addressEntries;

        sharedObjectAddressEntries.flush();

And after that I loop through the array to display each age. The important thing to note is that only the ages set during the current program load. All of the ones from previous runs are saved in that the array is still filled with People, but all of their ages are now set to 0.

Any help with this would be greatly appreciated, nothing I seemed to find has helped.

Upvotes: 0

Views: 1383

Answers (2)

KennyRules
KennyRules

Reputation: 221

Okay, after entering the correct terms in Google I found this nugget:

"If a class does not implement, nor inherits from a class which implements, the IExternalizable interface, then an instance of the class will be serialized using the default mechanism of public members only. As a result, private, internal, and protected members of a class will not be available."

So, that's why my private info doesn't save. The fix is to implement IExternalizable. Here's how I coded it, works like a charm:

public class Person implements IExternalizable
public function writeExternal(output:IDataOutput):void {

        output.writeInt(myAge);
    }

    public function readExternal(input:IDataInput):void {

        myAge = input.readInt();
    }

So easy now that I look at it, haha. Please note that I also needed to register the class alias too to save my custom class. Nothing major though. Hope somebody else can

Upvotes: 0

PatrickS
PatrickS

Reputation: 9572

This article should help you find a solution to your problem. http://jaycsantos.com/flash/the-trick-to-using-sharedobject/

check the "Don’t use direct reference to objects and use the game’s data only not the actual game objects." paragraph in particular

Upvotes: 1

Related Questions