Shared Objects: Saving Data in Air for iOS/Android

Looking for a solution to save data for your game or app that you are working on? If you are using Air, shared objects is one of the many solutions that you can use.

Shared Objects acts like a cookie. All you have to do to use it is, set it if you haven’t before and if you have, call it and send data to it, like below. Using shared objects is a great way to save data for user levels or the coordinates of where enemies are, so when the user comes back to the app, the enemies can remain in the same spot they were when the user quit.

mySo = SharedObject.getLocal(“application-name”);

Problems I Ran Into
While working on a side project, I thought using shared objects would be a easy solution that would do everything I need to do, however, their are a few tricks that you might want to be aware of before start using shared objects. Otherwise, you will run into so many problems that you will want to throw your laptop across the room and run over it back and forth until your run out of gas. If you want to use loop through individual pieces of data in a array that is stored in the shared object or want to save a class, you have to do it a certain way, which I have listed resources below in more detail.

Saving A Array/Class in a Shared Object
To do this, you need to create a array and send the data that you want saved into the array. Then, you want your shared object to save the array that you created, like below.

GameData is the variable of the class that I want to save.

var _gamedata:array;
_gamedata.push(‘GameData’);
mySo.data.mycategory = _gamedata;

Loop Through Individual Data
for (var i:int = 0; i < mySo.data.mycategory.length ; i++){
trace(mySo.data.mycategory[i].health);
}

The ‘mySo’ is the shared object. The ‘mycategory’ is the label, I guess you could say, that I want to save the data under. Inside ‘mySo’ shared object, I have a class and inside that class I have a variable named health.

Resources

  • http://stackoverflow.com/questions/6987932/how-to-properly-save-array-of-classes-via-storedobject-as3
  • http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/net/SharedObject.html#includeExamplesSummary
  • http://jaycsantos.com/flash/the-trick-to-using-sharedobject/#.UR_FymeneSo

Credits:

I would like to give credit to the people that posted on http://stackoverflow.com/questions/6987932/how-to-properly-save-array-of-classes-via-storedobject-as3 and and to http://jaycsantos.com/flash/the-trick-to-using-sharedobject/#.UR_FymeneSo.