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?

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)

ACTIONSCRIPT:
  1. lets_call_it_cookie = SharedObject.getLocal("feronato");
  2. if (lets_call_it_cookie.data.counter == undefined) {
  3.     lets_call_it_cookie.data.counter = 1;
  4. } else {
  5.     lets_call_it_cookie.data.counter++;
  6. }
  7. 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

ACTIONSCRIPT:
  1. lets_call_it_cookie = SharedObject.getLocal("feronato");
  2. levels = new Array();
  3. for (x=1; x<=5; x++) {
  4.     levels[x] = 0;
  5.     lev = _root.attachMovie("level", "level_"+x, _root.getNextHighestDepth(), {_x:80*x, _y:40, _alpha:30});
  6.     lev.levelnumber = x;
  7.     if (lets_call_it_cookie.data.completed_levels[x] == 1) {
  8.         lev._alpha = 100;
  9.     }
  10.     lev.onRelease = function() {
  11.         levels[this.levelnumber] = 1;
  12.         lets_call_it_cookie.data.completed_levels = levels;
  13.         lets_call_it_cookie.flush();
  14.         this._alpha = 100;
  15.     };
  16. }

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.

Improve the blog rating this post
Tell me what do you think about this post. I'll write better and better entries.
1 Star2 Stars3 Stars4 Stars5 Stars (3 votes, average: 5 out of 5)
Loading ... Loading ...

» Flash Templates provided by Template Monster are pre-made web design products developed using Flash technology.
They can be easily customized to meet the unique requirements of your project.

28 Responses to “Managing savegames with Flash shared objects”

  1. Frederik J on January 2nd, 2008 8:45 pm

    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.

  2. RJ on January 2nd, 2008 10:38 pm

    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

  3. sam on January 2nd, 2008 11:41 pm

    very very good post. Very useful and very easy.

  4. Kesh on January 3rd, 2008 4:25 am

    does this work on local? like can i test this on my comp or do i have to host it on the net?

  5. Anderson on January 3rd, 2008 6:19 am

    Very nice post I had been wondering how to do that for a while.

    @Kesh: this should work when you test on your computer

  6. Kesh on January 3rd, 2008 9:30 am

    ok i got it working. thanks.

  7. Wackiedk on January 3rd, 2008 10:36 am

    Very nice Emmanuele, I’ve been wondering for some time how to delete the SharedObject,
    because I thought it saved in cookies :D.

  8. shiv on January 3rd, 2008 11:59 am

    greatest!
    Why didnt you use it on tileball?

  9. RJ on January 3rd, 2008 1:37 pm

    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?

  10. RJ on January 3rd, 2008 3:53 pm

    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.

  11. And Mar on January 3rd, 2008 10:00 pm

    Emanuele, Gobble uses this method of save and retrieving game data such as current level, level scores, and personal highscore.

  12. And Mar on January 3rd, 2008 10:08 pm

    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.

  13. Emanuele Feronato on January 4th, 2008 1:16 am

    RJ
    you’re right, to fix this issue you should check your shared array when you start the movie and flush beaten levels.

  14. shiv on January 4th, 2008 10:27 am

    Hey Emanuele, hows tileball going on ?
    One week still not over?

  15. emanuel on January 5th, 2008 12:36 am

    so cool i was wondering how to this for long time tnx!!!!

  16. Shadow Scythe on January 5th, 2008 1:25 am

    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!

  17. RJ on January 5th, 2008 3:46 pm

    Thank you

  18. Josh on January 11th, 2008 8:46 pm

    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.

  19. Hawdon on January 12th, 2008 12:57 pm

    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!

  20. Emanuele Feronato on January 13th, 2008 2:16 am

    Josh: I carefully read all comments…

  21. quentin on January 16th, 2008 7:09 pm

    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

  22. Barney on January 26th, 2008 6:31 pm

    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

  23. brart on March 22nd, 2008 4:00 pm

    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

  24. brart on March 23rd, 2008 1:01 pm

    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

  25. souled on May 13th, 2008 10:46 pm

    Shouldn’t you use clear() instead of purge() ?
    purge() doesn’t do anything.

  26. Grifo on May 14th, 2008 12:08 am

    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.

  27. Creation of a Flash highscores API - Step 2 : Emanuele Feronato - italian geek and PROgrammer on May 15th, 2008 10:59 pm

    [...] 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 [...]

  28. souled on May 17th, 2008 6:44 pm

    This isn’t AS3…
    At least I don’t think so.

Leave a Reply