QuickTip: Managing Saved Data (SharedObject)

This entry is part of a series, Quick Tips»

Today we’re going to look at how to manage saved data, in flash called shared objects.

Usually that can be a pain, but thanks to Vlad from Vortix Games Studio, this is very simple. He created a class for this called LocalData, and it’s a super simple way of managing saved data.

First off you need to download the class, it’s a part of their Bold Pixel Engine – download it here.

Extract the files and put the ‘bpe’ folder in your library.

To use the class you need to import it:

import bpe.toolkit.LocalData;

As the class only operates with static functions, we don’t need to instantiate it – so we’re set for using the class.

Before we can save or load data, we need to create or load a file.

This is done by calling:

LocalData.create ( Name ) ;

The ‘Name’ needs to be replaced with a String of your own choice. Be sure to call it something unique, if you just call it “Data”, and I made an application which also uses “Data”, we would store data to the same file, thereby messing everything up.

Saving Data

When you want to save data you need to supply a positive whole number (uint) as reference for where the data is stored, and the actual data (which can by anything). This could be the name of the user:

LocalData.write ( 1, "Rasmus Wriedt Larsen") ;

I would recommend saving these reference numbers in your document class as static constants, like this:

public static const USER_NAME:uint = 1;

Then I don’t need to remember which one is the user name, which one is the volume settings and so on… using this, the code would look like:

LocalData.write ( Main.USER_NAME, "Rasmus Wriedt Larsen") ;

(if my document class was called “Main”)

Loading Data

To load data we need to provide the reference number:

LocalData.read ( Main.USER_NAME ) ; // or use 1 instead of Main.USER_NAME

This can return anything, as we can save anything. If nothing is there, it will return undefined. This code will do different things, depending on if we have a stored user name:

if ( LocalData.read ( Main.USER_NAME ) == undefined ) // or use 1 instead of Main.USER_NAME
	// make the user type in his name, and afterwards save it
	// display the user name

Deleting Everything

If you want to delete everything you have saved, thereby reseting the file, just call:

LocalData.reset ( ) ;

If you just want to delete one thing, just assing it to being undefined:

LocalData.write ( Main.USER_NAME, undefined ) ;


Happy coding :)

Posted on February 19th, 2010
Filed under ActionScript 3, Tutorial |

4 Responses to “QuickTip: Managing Saved Data (SharedObject)”

  1. Vlad
    February 19th, 2010 at 22:28

    Hey mate :) Feels really good to see a tut of our framework, thanks for that.

  2. New Bold Pixel Engine tutorial |
    February 19th, 2010 at 22:32

    […] Take a look. […]

  3. Travis
    March 22nd, 2010 at 00:45

    What makes this different than a SharedObject?

  4. Rasmus Wriedt Larsen
    March 27th, 2010 at 17:48

    Hey travis. Sorry I haven’t replied earlier.
    This method is actually using SharedObject, basicly this is just an easier way handing them. If you want to learn a little bit about saving and loading data without the LocalData class from Vortix Games Stuido, you can take a look at this part of Michael James Williams tutorial: http://gamedev.michaeljameswilliams.com/2009/03/18/avoider-game-tutorial-11/

Leave a Reply