00:00
00:00
Newgrounds Background Image Theme

MisterBig67425 just joined the crew!

We need you on the team, too.

Support Newgrounds and get tons of perks for just $2.99!

Create a Free Account and then..

Become a Supporter!

AS: Save and Load

36,879 Views | 127 Replies
New Topic Respond to this Topic

AS: Save and Load 2005-02-25 10:17:16


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

Response to AS: Save and Load 2005-02-25 10:21:50


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

Response to AS: Save and Load 2005-02-25 10:26:54


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

Response to AS: Save and Load 2005-03-16 15:44:58


great post!

Response to AS: Save and Load 2005-03-16 15:54:41


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.

Response to AS: Save and Load 2005-03-16 16:41:17


Thank you...
*bookmarks*

Response to AS: Save and Load 2005-03-16 16:51:07


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

Response to AS: Save and Load 2005-03-16 16:58:47


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

Response to AS: Save and Load 2005-03-16 19:43:13


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

Response to AS: Save and Load 2005-03-16 19:53:14


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.

Response to AS: Save and Load 2005-03-16 20:03:32


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

Response to AS: Save and Load 2005-03-16 21:54:07


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

Response to AS: Save and Load 2005-03-16 22:03:42


thanks. 100k is alot :o


BBS Signature

Response to AS: Save and Load 2005-03-16 22:05:46


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

Response to AS: Save and Load 2005-03-16 22:40:24


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

Response to AS: Save and Load 2005-03-16 22:42:11


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

Response to AS: Save and Load 2005-03-16 22:46:32


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

Response to AS: Save and Load 2005-03-16 22:57:01


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.

Response to AS: Save and Load 2005-04-05 13:14:12


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

Response to AS: Save and Load 2005-06-04 10:37:18


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

Response to AS: Save and Load 2005-06-07 01:12:48


Who didn't know this? besides noobs


wtfbbqhax

Response to AS: Save and Load 2005-06-07 02:42:00


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

Response to AS: Save and Load 2005-06-07 04:18:14


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

Response to AS: Save and Load 2005-06-07 11:26:01


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


BBS Signature

Response to AS: Save and Load 2005-06-07 12:53:36


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

Response to AS: Save and Load 2005-06-08 18:50:12


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


ey

BBS Signature

Response to AS: Save and Load 2005-06-08 19:05:01


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

Response to AS: Save and Load 2005-06-10 12:23:45


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

Response to AS: Save and Load 2005-07-04 11:42:33


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!


BBS Signature

Response to AS: Save and Load 2005-07-04 13:33:01


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