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)

1
2
3
4
5
6
7
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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.

Rate this post: 1 Star2 Stars3 Stars4 Stars5 Stars (18 votes, average: 4.78 out of 5)
Loading ... Loading ...
If you found this post useful, please consider a small donation.
» 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.

33 Responses

  1. Frederik J says:

    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 says:

    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 says:

    very very good post. Very useful and very easy.

  4. Kesh says:

    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 says:

    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 says:

    ok i got it working. thanks.

  7. Wackiedk says:

    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 says:

    greatest!
    Why didnt you use it on tileball?

  9. RJ says:

    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 says:

    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 says:

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

  12. And Mar says:

    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 says:

    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 says:

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

  15. emanuel says:

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

  16. 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. Josh says:

    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.

  18. Hawdon says:

    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!

  19. Emanuele Feronato says:

    Josh: I carefully read all comments…

  20. quentin says:

    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

  21. Barney says:

    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

  22. brart says:

    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

  23. brart says:

    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

  24. souled says:

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

  25. Grifo says:

    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.

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

  27. souled says:

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

  28. ADAM says:

    DEAR Emanuele Feronato,
    I need your help to tell me the way of saving results in Zuma game because the results are kept even if the game is moved to a different place.So,it is different from shared objects

  29. Josh says:

    This may be an old post but im trying to make something like this but a little less complicated
    this is what i have so far

    ballsave = SharedObject.getLocal(“ballgame”);
    if (ballsave.data.lvl1 == undefined) {
    ballsave.data.counter = false;
    }
    lvl1=ballsave.data.lvl1;

    When you beat lvl1 lvl1 = true
    but for some reason the variable stays undefined,
    what am i doing wrong?

  30. Joel Parkey says:

    This post prooved very helpful. Thanks. Its allowed me to have a locally saved Highscore system instead of using someone elses highscore API. I decided to add the clear highscores option aswell should you need to empty your highscores and start again !

    Thanks again!

  31. [...] This is the AS3 version of Managing savegames with Flash shared objects. [...]

    • Daniele Fernandes says:

      First, I have found in web some codes that produce similar effects, but they are too complicated and I can’t understand them. The codes in your page are simple and work fine. So, thanks very much!
      But I am trying hard to make an application in which the user can drag the movies (created by attachMovie) and when he/she release it, the position – of each – movie is saved in the sharedObject. Now I just can save the position of the last generated movie and every “beated movie” go exataly behind that when I open the file again. I know it can be too easy for you, but I am new in action script. If anyone could help me, I would be extremely grateful… Thanks in advance. The code:
      lets_call_it_cookie = SharedObject.getLocal(“feronato”);
      levels = new Array();
      for (x=1; x<=5; x++) {
      levels[x] = 0; //unbeating
      lev = _root.attachMovie("level", "level_"+x,_root.getNextHighestDepth(),{_x:80*i, _y:40, _alpha:30});
      lev.levelnumber = x;
      if (lets_call_it_cookie.data.completed_levels[x] == 1) { //beating
      lev._x = lets_call_it_cookie.data.x;
      lev._y = lets_call_it_cookie.data.y;
      // lev._alpha = 100;
      levels[x] = 1;
      }
      lev.onPress = function(){
      stopper = "yes";
      this.startDrag();
      }
      lev.onRelease = function() {
      stopper = "no";
      this.stopDrag();
      levels[this.levelnumber] = 1;
      lets_call_it_cookie.data.completed_levels = levels;
      lets_call_it_cookie.data.x = lev._x;
      lets_call_it_cookie.data.y = lev._y;
      lets_call_it_cookie.flush();
      // this._alpha = 100;
      }
      }

Leave a Reply