QuickTip: Managing Saved Data (SharedObject)
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 } else { // 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 :)
- QuickTip: Execute Code After Animaiton
- QuickTip: Scrolling Only in SWF, not on Page.
- QuickTip: Managing Saved Data (SharedObject)
- QuickTip: Understanding Right Click Menus (ContextMenu)
- QuickTip: Stopping an Event
- QuickTip: Avoid Firefox popup-blocker When Using navigateToUrl
- QuickTip: Inline If-Then-Else Statement
- QucikTip: Trace an Array With Correct Formatting