Managing savegames with Flash shared objects
How many times did you find an option to save the game in Flash? How many times did you see a game that keeps track of your previously solved levels, like Nitrome's Snow Drift?
Today you'll learn how to do it... and it's very simple!
We can do it with just a couple of instructions thanks to the SharedObject class. This class is used to read and store limited amounts of data on a user's computer. The class does not create cookies, but something very similar, and the concept is the same.
It's very important to understand that local shared objects maintain local persistence. This means that you can play a game, beat some levels, save the game, turn off your computer, and next time you'll play that game on the same computer, you'll start from the first unbeaten level.
But... less words and more action(script)
-
lets_call_it_cookie = SharedObject.getLocal("feronato");
-
if (lets_call_it_cookie.data.counter == undefined) {
-
lets_call_it_cookie.data.counter = 1;
-
} else {
-
lets_call_it_cookie.data.counter++;
-
}
-
counter_text.text = "You visited this page "+lets_call_it_cookie.data.counter+" times!";
Line 1: A variable called lets_call_it_cookie gets the reference to a locally shared object called feronato. If the shared objects does not exists, a new one is created.
Line 2: Looking for a variable called counter inside the shared object
Line 3: If the variable does not exists (it's the first time we are running the movie), then set the counter variable inside the shared object to 1
Line 5: If the variable exists (it's not the first time we are running the movie), then increase the counter variable
Line 6: Display some text on a dynamic text I created
And this is the result:
Try to reload the page and see what happens... then close the page and open it again, or shut down your computer and come back to this page...
Now, I have to warn you about some important local disk space considerations I found on Adobe website: Local shared objects can be very useful, but they have some limitations that are important to consider as you design your application. Sometimes your SWF files may not be allowed to write local shared objects, and sometimes the data stored in local shared objects can be deleted without your knowledge. Flash Player users can manage the disk space that is available to individual domains or to all domains. When users lower the amount of disk space available, some local shared objects may be deleted. Flash Player users also have privacy controls that can prevent third-party domains (domains other than the domain in the current browser address bar) from reading or writing local shared objects.
Let's see another actionscript
-
lets_call_it_cookie = SharedObject.getLocal("feronato");
-
levels = new Array();
-
for (x=1; x<=5; x++) {
-
levels[x] = 0;
-
lev = _root.attachMovie("level", "level_"+x, _root.getNextHighestDepth(), {_x:80*x, _y:40, _alpha:30});
-
lev.levelnumber = x;
-
if (lets_call_it_cookie.data.completed_levels[x] == 1) {
-
lev._alpha = 100;
-
}
-
lev.onRelease = function() {
-
levels[this.levelnumber] = 1;
-
lets_call_it_cookie.data.completed_levels = levels;
-
lets_call_it_cookie.flush();
-
this._alpha = 100;
-
};
-
}
Line 1: Same as before
Line 2: Creating an array that will contain levels
Line 3: Beginning of a loop scanning through the 5 levels in the game
Line 4: Setting the x-th levels to zero (unbeaten)
Line 5: Attaching the movieclip that should represent the level icon
Line 6: Assigning a number to the previously created icon
Line 7: Looking for a variable called completed_levels[x] inside the shared object. I am checking if the variable is set to one (beaten)
Line 8: In this case, set the alpha of the level to 100 (it was previously set to 30 at line 5)
Line 10: Beginning of the actions to be performed every time you click on a level icon (you beat a level simply clicking on its icon)
Line 11: Updating levels array
Line 12: Copying the levels array into the completed_levels variable inside the shared object
Line 13: Immediately writes a locally persistent shared object to a local file with the flush method
Line 14: Set the alpha of the level icon to 100 (beaten)
Now beat a couple of levels and reload the page or do one of the operations I've told you before
This is what you have to do to manage your savegames
To clear your shared object just use the purge method.
lets_call_it_cookie.purge() purges all the data from the shared object and deletes the shared object from the disk.
Download the source and enjoy.
Tell me what do you think about this post. I'll write better and better entries.
They can be easily customized to meet the unique requirements of your project.
28 Responses to “Managing savegames with Flash shared objects”
Leave a Reply


Wow.. Really nice Emanuele. It’s fun, how you can make a post about what I need to find, WHEN I need to find it!
It’s nice, and I’m glad you’ve told us how to. I’ve searched all the web for a function like that, but I haven’t managed to find one yet, with a LIVE example.
Thanks.
Just what I needeed! It’s great!
When a game can be saved, I think “Good game”. Now you showed us all how to do it.
At first sight is difficult, but with your explanations I finally understood it
One of the most useful posts
very very good post. Very useful and very easy.
does this work on local? like can i test this on my comp or do i have to host it on the net?
Very nice post I had been wondering how to do that for a while.
@Kesh: this should work when you test on your computer
ok i got it working. thanks.
Very nice Emmanuele, I’ve been wondering for some time how to delete the SharedObject,
because I thought it saved in cookies :D.
greatest!
Why didnt you use it on tileball?
I’ve been thinking about it, and it’s better to give your shared object an strange name because otherwise, someone could hack it.For example, if I make a swf with this actionscript:
lets_call_it_cookie = sharedObject.getLocal(”feronato”);
if (lets_call_it_cookie.data.counter <= 500 undefined) {
lets_call_it_cookie.data.counter = 500;
}
Then your counter would show me “You visited this page 501 times”
Am I right?
Something doesn’t work very well with the second example. Try checking 3 or 4, then reload the page, then check another one and reload the page. Only the last one you checked remains beaten.
Emanuele, Gobble uses this method of save and retrieving game data such as current level, level scores, and personal highscore.
RJ
That is a good point. I thought that would work…until I tried it. Only a .swf from the original .swf domain can access that Shared Object.
RJ
you’re right, to fix this issue you should check your shared array when you start the movie and flush beaten levels.
Hey Emanuele, hows tileball going on ?
One week still not over?
so cool i was wondering how to this for long time tnx!!!!
Yes! This is exactly what I need! Er, no it isn’t. But I needed it at one point, and surely will later. I’ll keep it in mind, and remember to credit you. Thanks!
Thank you
Lol, this is kewl
i think i asked if you could do this tut a while ago nice to know that someone actually reads what you say.
Or mabey it was a coincidence?
man, i just ruined my happy thought.
Hi Emanuele,
I was wondering, how can you do so that you have to have clicked on the “level one” box to be able to click on the “level two” box and so on?
Thanks,
and a relay nice tutorial btw!
Josh: I carefully read all comments…
hi
i like programing games
and i am right now programing a month game that is very long, i am about 2300 ligns of programme and i would like to know how to use the actionscript file.
if you could make a tutorial or send me an e-mail, it would be much of a help for me
thanks
Hi,
This is a great tutorial, but the onl problem I’m having at the moment is clearing the shared Object in flash 7/mx.
The purge method only seems to work on flash8!!!
Barney
here is the solution for the save problem from example 2. Replace it with this.
if (gamedata.data.completed_levels[x] == 1) {
lev._alpha = 100;
levels[x] = 1;
}
brart
sorry… it’s not: gamedata.data.completed_levels[x]
but
lets_call_it_cookie.data.completed_levels[x]
I used the gamedata variable instead of the lets_call_it_cookie
so the final code would be:
lets_call_it_cookie = SharedObject.getLocal(”feronato”);
levels = new Array();
for (x=1; x<=5; x++) {
levels[x] = 0;
lev = _root.attachMovie(”level”, “level_”+x, _root.getNextHighestDepth(), {_x:80*x, _y:40, _alpha:30});
lev.levelnumber = x;
if (lets_call_it_cookie.data.completed_levels[x] == 1) {
lev._alpha = 100;
levels[x] = 1;
}
lev.onRelease = function() {
levels[this.levelnumber] = 1;
lets_call_it_cookie.data.completed_levels = levels;
lets_call_it_cookie.flush();
this._alpha = 100;
};
}
This is the fix for the save problem in the second example. I hope you can use it.
brart
Shouldn’t you use clear() instead of purge() ?
purge() doesn’t do anything.
I might be wrong, Souled, but maybe this is as3 and you’re in as2… I’m in a hurry now so I’m no sure.
[...] hard prototype once you read Create an Eskiv Flash game tutorial (in order to have a real game) and Managing savegames with Flash shared objects (to store data on your [...]
This isn’t AS3…
At least I don’t think so.