Be a Supporter!

AS: Save and Load

  • 25,566 Views
  • 122 Replies
New Topic Respond to this Topic
Denvish
Denvish
  • Member since: Apr. 25, 2003
  • Offline.
Forum Stats
Member
Level 46
Blank Slate
AS: Save and Load Feb. 25th, 2005 @ 10:17 AM Reply

AS: Save and Load

A nice quick and easy one.

What?

Flash allows you to store a 'cookie' on the local machine. These are .sol files, and on WinXP, they can be found at:
C:/Documents and Settings/Username/Application Data (hidden)/Macromedia/Flash Player/

--------------------------------------------

Loading

A simple example: Score and Level.
This example will autoload old score/level when the game is started
Add this at the start of your main timeline:

//Specify the save file to use
var savefile = SharedObject.getLocal("yourgamename");
//Grab the level and score held in the file, and assign them to variables
_root.oldscore = savefile.data.score;
_root.oldlevel = savefile.data.level;
//If they don't exist, (ie first run), set variables to zero
if(savefile.data.score==undefined){
_root.oldscore=0;
_root.oldlevel=0;
}

Same thing, but on a LOAD button

on (press){
var savefile = SharedObject.getLocal("yourgamename");
if(savefile.data.score==undefined){
_root.score=0;
_root.level=0;
}else{
_root.score=savefile.data.score;
_root.level=savefile.data.level;
}
}

------------------------------------------

Saving

Assume that your game score is held in the variable "_root.score", and the current level is "_root.level".
Now put a SAVE button where you want to allow saving, and add these actions:

on (press){
savefile.data.score=_root.score;
savefile.data.level=_root.level;
savefile.flush();
}

-----------------------------------------

And that's it. You can send text and numbers to the savefile, using script variables or text input. Be wary of hexadecimal colour values, I've had problems with them.

Other Save/Load information:
http://www.newgrounds.com/portal/view/201218
http://www.macromedia.com/supp...ect/local_shared_object05.html
http://www.macromedia.com/supp...ctionscript_dictionary648.html
http://www.actionscripts.org/t...iate/SharedObjects/index.shtml
http://www.flashguru.co.uk/000022.php
http://www.flashmagazine.com/html/634.htm

AS: Sections
AS: Main
AS: Basic Movement
AS: Random Movement
AS: Sound
AS: Preloader


- - Flash - Music - Images - -

BBS Signature
TehBanStick
TehBanStick
  • Member since: Apr. 13, 2004
  • Offline.
Forum Stats
Member
Level 17
Blank Slate
Response to AS: Save and Load Feb. 25th, 2005 @ 10:21 AM Reply

Wow, I've been working with Flash for 2 years and didn't even know this was possible. Thanks alot, I think I'll start a new project now:)


abcdefghijklmnopqrstuvwxyz

Denvish
Denvish
  • Member since: Apr. 25, 2003
  • Offline.
Forum Stats
Member
Level 46
Blank Slate
Response to AS: Save and Load Feb. 25th, 2005 @ 10:26 AM Reply

At 2/25/05 10:21 AM, TCStyle wrote: Wow, I've been working with Flash for 2 years and didn't even know this was possible. Thanks alot, I think I'll start a new project now:)

It's actually not widely advertised, and I think it may have only been implemented in FlashMX. The only reason I know about it is because a few games around the time of Punk-o-Matic started showing save & load options, and I was intrigued... but I couldn't find anything about it on the net, so I posted on the Flashkit forums, and a couple of users very kindly posted the code.
Since I found out about it, it finds its way into virtually every Flash project. It's very useful.


- - Flash - Music - Images - -

BBS Signature
raitendo
raitendo
  • Member since: Feb. 19, 2001
  • Offline.
Forum Stats
Member
Level 24
Blank Slate
Response to AS: Save and Load Mar. 16th, 2005 @ 03:44 PM Reply

great post!

legendary-frog
legendary-frog
  • Member since: Dec. 29, 2004
  • Offline.
Forum Stats
Member
Level 08
Blank Slate
Response to AS: Save and Load Mar. 16th, 2005 @ 03:54 PM Reply

it was a good what i don't understand is why you don't make tutorials, yoou make loads of topics teaching people how to make stuff, that self made movie clip mp3 mjiggy whadjamacllit thing was good and helped me alot, thanks for that but i think you should make a tutorial.

szmuk
szmuk
  • Member since: Dec. 10, 2003
  • Offline.
Forum Stats
Member
Level 07
Blank Slate
Response to AS: Save and Load Mar. 16th, 2005 @ 04:41 PM Reply

Thank you...
*bookmarks*

Denvish
Denvish
  • Member since: Apr. 25, 2003
  • Offline.
Forum Stats
Member
Level 46
Blank Slate
Response to AS: Save and Load Mar. 16th, 2005 @ 04:51 PM Reply

At 3/16/05 03:54 PM, SnoopHog wrote: it was a good what i don't understand is why you don't make tutorials, yoou make loads of topics teaching people how to make stuff, that self made movie clip mp3 mjiggy whadjamacllit thing was good and helped me alot, thanks for that but i think you should make a tutorial.

I did make a tut for the self-contained Movie Control MC, but it died along with the old FlashRegs site, and I didn't have a backup copy


- - Flash - Music - Images - -

BBS Signature
Afro-Ninja
Afro-Ninja
  • Member since: Mar. 2, 2002
  • Offline.
Forum Stats
Moderator
Level 44
Game Developer
Response to AS: Save and Load Mar. 16th, 2005 @ 04:58 PM Reply

Shared objects came about in flash MX. It isn't documented in flash's AS dictionary. I found out about the same way you did- saw the save feature appearing in games so I found documentation on it at macromedia's website, as well as a couple supplementary tutorials

When I get NG sim done I should show you my method for a multiple-save file system =)


BBS Signature
Juice-Tin
Juice-Tin
  • Member since: Feb. 3, 2001
  • Offline.
Forum Stats
Member
Level 09
Game Developer
Response to AS: Save and Load Mar. 16th, 2005 @ 07:43 PM Reply

sorry to steal your thunder afro, but i havnt posted yet and i need my fix.
here your saving all your stuff into, lets just say a folder named yourgamename.
var savefile = SharedObject.getLocal("yourgamename");

to have multiple save files, just turn that into a variable.

var savefile = SharedObject.getLocal(saveFile);

you can then make new "folders" using that variable to save all your stuff in.
For example, putting this,
saveFile="save1";
var savefile = SharedObject.getLocal(saveFile);
and then running all the save code will save everything into the save1 file, or
saveFile="save5";
var savefile = SharedObject.getLocal(saveFile);
and then running all the save code will save everything into the save5 file. Easy as pie.
Sorry again afroninja, but i havnt answered an AS question in ages, and this was like a trip to the candy store for me.


BBS Signature
severed-fingers
severed-fingers
  • Member since: Sep. 12, 2002
  • Offline.
Forum Stats
Member
Level 03
Blank Slate
Response to AS: Save and Load Mar. 16th, 2005 @ 07:53 PM Reply

it does have it's limitations though , it can only save a certain amount of data, and if the limit is reached you will need to increase the ammount of data that the flash player can hold, but it's quite a bit before it's filled, but i believe i reached it before.

Juice-Tin
Juice-Tin
  • Member since: Feb. 3, 2001
  • Offline.
Forum Stats
Member
Level 09
Game Developer
Response to AS: Save and Load Mar. 16th, 2005 @ 08:03 PM Reply

really? ived saved up to about 60+ variables (mostly int) in one of my flash games without any problems. How much data do you think you had? And how did you know you reached the limit?


BBS Signature
Afro-Ninja
Afro-Ninja
  • Member since: Mar. 2, 2002
  • Offline.
Forum Stats
Moderator
Level 44
Game Developer
Response to AS: Save and Load Mar. 16th, 2005 @ 09:54 PM Reply

At 3/16/05 08:03 PM, Juice-Tin wrote: really? ived saved up to about 60+ variables (mostly int) in one of my flash games without any problems. How much data do you think you had? And how did you know you reached the limit?

Actually, I think your method is better. Well, what I do is create five local objects. I create a shared object. Then I used a variable to keep track of what file I'm in (1-5)

I also assign each of the five local objects to the sharedObject objects

MAINFILE = SharedObject.getLocal("Save Files");
MAINFILE.data.f1= File1()
MAINFILE.data.f2= File2()
MAINFILE.data.f3= File3()
MAINFILE.data.f4= File4()
MAINFILE.data.f5= File5()

MAINFILE.flush();

then to save files I simply reference the local objects:

var workingfilename= "File"+filenum
_root[_root.filenum].alias="Afro_Ninja"

the files are written automatically. Defualt storage for shared objects is 100k. If it goes over the limit it will prompt the user to allow more. To see this yourself right click the flash and select "settings" or to do it with AS just type

System.showSettings(1)

AS: Save and Load


BBS Signature
Juice-Tin
Juice-Tin
  • Member since: Feb. 3, 2001
  • Offline.
Forum Stats
Member
Level 09
Game Developer
Response to AS: Save and Load Mar. 16th, 2005 @ 10:03 PM Reply

thanks. 100k is alot :o


BBS Signature
Juice-Tin
Juice-Tin
  • Member since: Feb. 3, 2001
  • Offline.
Forum Stats
Member
Level 09
Game Developer
Response to AS: Save and Load Mar. 16th, 2005 @ 10:05 PM Reply

hey you seem to know as. Im not sure if you saw my AS post, so here it is,
http://newgrounds.com/bbs/topic.php?id=243979
think you can help me out?


BBS Signature
Afro-Ninja
Afro-Ninja
  • Member since: Mar. 2, 2002
  • Offline.
Forum Stats
Moderator
Level 44
Game Developer
Response to AS: Save and Load Mar. 16th, 2005 @ 10:40 PM Reply

At 3/16/05 10:05 PM, Juice-Tin wrote: hey you seem to know as. Im not sure if you saw my AS post, so here it is,
http://newgrounds.com/bbs/topic.php?id=243979
think you can help me out?

eh, I was never too good with swapDepths : (


BBS Signature
Idoru
Idoru
  • Member since: Jan. 11, 2002
  • Offline.
Forum Stats
Member
Level 02
Blank Slate
Response to AS: Save and Load Mar. 16th, 2005 @ 10:42 PM Reply

local shared object is ok, but remote shared object is 10x the capabilities.

Afro-Ninja
Afro-Ninja
  • Member since: Mar. 2, 2002
  • Offline.
Forum Stats
Moderator
Level 44
Game Developer
Response to AS: Save and Load Mar. 16th, 2005 @ 10:46 PM Reply

At 3/16/05 10:42 PM, Idoru wrote: local shared object is ok, but remote shared object is 10x the capabilities.

never heard of em. care to elaborate? Is this new to flash mx 04?


BBS Signature
Idoru
Idoru
  • Member since: Jan. 11, 2002
  • Offline.
Forum Stats
Member
Level 02
Blank Slate
Response to AS: Save and Load Mar. 16th, 2005 @ 10:57 PM Reply

Unfortunately SharedObjects only work with flash communication server, but it is the sole basis for multiuser environments, like chat.

essentially what happens is that the sharedobject is created on the server and it has an onSync function that triggers whenever the sharedobject is changed and all the clients that are connected to the sharedobject can view the change.

s1nt3ch
s1nt3ch
  • Member since: Jul. 2, 2002
  • Offline.
Forum Stats
Member
Level 09
Blank Slate
Response to AS: Save and Load Apr. 5th, 2005 @ 01:14 PM Reply

At 3/16/05 08:03 PM, Juice-Tin wrote: really? ived saved up to about 60+ variables (mostly int) in one of my flash games without any problems. How much data do you think you had? And how did you know you reached the limit?

if you right click flash and go to settings then go to memory you can see the default amount of k that is reserved for one specific flash file, while this can be increased manually , if you reach the limit with shared objects, an annoying window will pop up asking if you want to increase the ammount of memory that flash can use for this site. Does anyone knw a work around?


BBS Signature
BluArchon
BluArchon
  • Member since: May. 28, 2005
  • Offline.
Forum Stats
Member
Level 07
Blank Slate
Response to AS: Save and Load Jun. 4th, 2005 @ 10:37 AM Reply

This thread is neat! I wasn't planning to create this feature in my game but after seeing this I immediately incorporated it there.

fwe
fwe
  • Member since: Jul. 24, 2003
  • Offline.
Forum Stats
Member
Level 08
Blank Slate
Response to AS: Save and Load Jun. 7th, 2005 @ 01:12 AM Reply

Who didn't know this? besides noobs


wtfbbqhax

Toast
Toast
  • Member since: Apr. 2, 2005
  • Offline.
Forum Stats
Member
Level 09
Blank Slate
Response to AS: Save and Load Jun. 7th, 2005 @ 02:42 AM Reply

At 2/25/05 10:17 AM, Denvish wrote:

:Boring stuf
Thanks,Denvish.That quite helped.I never bothered to look at load and save tutorials in flashkit.I will try to make a load and save for my RPG,people likes games much more when they can 'control' the game more freely.
I was always wondering why my
"AS:<stuff>"
Threads got only about two replys...Maybe it is because you are a moderator!


BBS Signature
Rustygames
Rustygames
  • Member since: May. 7, 2005
  • Offline.
Forum Stats
Member
Level 19
Programmer
Response to AS: Save and Load Jun. 7th, 2005 @ 04:18 AM Reply

At 6/7/05 01:12 AM, SHITTYFLASHMAN wrote: Who didn't know this? besides noobs

Why do you always have to be so mean?

And yeah thanks Denvish you always post the best threads keep it up man your definatly my faverout mod (You actually help people unlike the others)


- Matt, Rustyarcade.com

Toast
Toast
  • Member since: Apr. 2, 2005
  • Offline.
Forum Stats
Member
Level 09
Blank Slate
Response to AS: Save and Load Jun. 7th, 2005 @ 11:26 AM Reply

I tried your code out,it doesn't work...


BBS Signature
Rustygames
Rustygames
  • Member since: May. 7, 2005
  • Offline.
Forum Stats
Member
Level 19
Programmer
Response to AS: Save and Load Jun. 7th, 2005 @ 12:53 PM Reply

At 6/7/05 11:26 AM, Dark_Toaster wrote: I tried your code out,it doesn't work...

It worked brilliantly for me so your obviously not doing exactly what he said... You can go read some tutorials if you are unsure on how to navigate flash


- Matt, Rustyarcade.com

Joelasticot
Joelasticot
  • Member since: Feb. 14, 2003
  • Offline.
Forum Stats
Supporter
Level 46
Game Developer
Response to AS: Save and Load Jun. 8th, 2005 @ 06:50 PM Reply

Is it possible to save/load arrays and all their values just like you save variables?


ey

BBS Signature
fwe
fwe
  • Member since: Jul. 24, 2003
  • Offline.
Forum Stats
Member
Level 08
Blank Slate
Response to AS: Save and Load Jun. 8th, 2005 @ 07:05 PM Reply

At 6/8/05 06:50 PM, Joelasticot wrote: Is it possible to save/load arrays and all their values just like you save variables?

Isn't an array a variable, in a way?

So, yes


wtfbbqhax

epheph
epheph
  • Member since: Mar. 31, 2005
  • Offline.
Forum Stats
Member
Level 12
Blank Slate
Response to AS: Save and Load Jun. 10th, 2005 @ 12:23 PM Reply

At 3/16/05 10:57 PM, Idoru wrote: Unfortunately SharedObjects only work with flash communication server, but it is the sole basis for multiuser environments, like chat.

essentially what happens is that the sharedobject is created on the server and it has an onSync function that triggers whenever the sharedobject is changed and all the clients that are connected to the sharedobject can view the change.

I just found a GREAT project called PHPObject, which allows you to share a single instance of an object between PHP & ActionScript. It makes dealing with server-side communication relatively easy, while still being free and GPL...

PHPObject Site

Hoeloe
Hoeloe
  • Member since: Apr. 29, 2004
  • Offline.
Forum Stats
Member
Level 37
Game Developer
Response to AS: Save and Load Jul. 4th, 2005 @ 11:42 AM Reply

mine dosnt work! when i click load, it only loads the level, not the score, and when i do that, it just loads the variables, not takes me back to the old part of the game!


Decima: The Last Story of Vald has a Facebook page and a development blog. Give them a look!
------------------------------

BBS Signature
Denvish
Denvish
  • Member since: Apr. 25, 2003
  • Offline.
Forum Stats
Member
Level 46
Blank Slate
Response to AS: Save and Load Jul. 4th, 2005 @ 01:33 PM Reply

At 7/4/05 11:42 AM, Hoeloe wrote: mine dosnt work! when i click load, it only loads the level, not the score, and when i do that, it just loads the variables, not takes me back to the old part of the game!

Is your score held in the variable '_root.score' ?
Have you told it to 'gotoAndPlay(level)' once 'level' is loaded from the savefile?
You'll need to adapt the script a little if neither of these is the case


- - Flash - Music - Images - -

BBS Signature