Forum Topic: AS: Save and Load

(15,925 views • 119 replies)

This topic is 4 pages long. [ 1 | 2 | 3 | 4 ]

<< < > >>
None

Denvish

Reply To Post Reply & Quote

Posted at: 2/25/05 10:17 AM

Denvish DARK LEVEL 46

Sign-Up: 04/25/03

Posts: 16,236

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

None

TehBanStick

Reply To Post Reply & Quote

Posted at: 2/25/05 10:21 AM

TehBanStick LIGHT LEVEL 17

Sign-Up: 04/13/04

Posts: 2,698

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


None

Denvish

Reply To Post Reply & Quote

Posted at: 2/25/05 10:26 AM

Denvish DARK LEVEL 46

Sign-Up: 04/25/03

Posts: 16,236

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

None

raitendo

Reply To Post Reply & Quote

Posted at: 3/16/05 03:44 PM

raitendo NEUTRAL LEVEL 23

Sign-Up: 02/19/01

Posts: 437

great post!


None

legendary-frog

Reply To Post Reply & Quote

Posted at: 3/16/05 03:54 PM

legendary-frog NEUTRAL LEVEL 08

Sign-Up: 12/29/04

Posts: 1,004

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.


None

szmuk

Reply To Post Reply & Quote

Posted at: 3/16/05 04:41 PM

szmuk NEUTRAL LEVEL 07

Sign-Up: 12/10/03

Posts: 93

Thank you...
*bookmarks*


None

Denvish

Reply To Post Reply & Quote

Posted at: 3/16/05 04:51 PM

Denvish DARK LEVEL 46

Sign-Up: 04/25/03

Posts: 16,236

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

None

Afro-Ninja

Reply To Post Reply & Quote

Posted at: 3/16/05 04:58 PM

Afro-Ninja EVIL LEVEL 37

Sign-Up: 03/02/02

Posts: 13,464

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

None

Juice-Tin

Reply To Post Reply & Quote

Posted at: 3/16/05 07:43 PM

Juice-Tin LIGHT LEVEL 09

Sign-Up: 02/03/01

Posts: 852

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

None

severed-fingers

Reply To Post Reply & Quote

Posted at: 3/16/05 07:53 PM

severed-fingers EVIL LEVEL 03

Sign-Up: 09/12/02

Posts: 482

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.


None

Juice-Tin

Reply To Post Reply & Quote

Posted at: 3/16/05 08:03 PM

Juice-Tin LIGHT LEVEL 09

Sign-Up: 02/03/01

Posts: 852

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

None

Afro-Ninja

Reply To Post Reply & Quote

Posted at: 3/16/05 09:54 PM

Afro-Ninja EVIL LEVEL 37

Sign-Up: 03/02/02

Posts: 13,464

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

None

Juice-Tin

Reply To Post Reply & Quote

Posted at: 3/16/05 10:03 PM

Juice-Tin LIGHT LEVEL 09

Sign-Up: 02/03/01

Posts: 852

thanks. 100k is alot :o

BBS Signature

None

Juice-Tin

Reply To Post Reply & Quote

Posted at: 3/16/05 10:05 PM

Juice-Tin LIGHT LEVEL 09

Sign-Up: 02/03/01

Posts: 852

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

None

Afro-Ninja

Reply To Post Reply & Quote

Posted at: 3/16/05 10:40 PM

Afro-Ninja EVIL LEVEL 37

Sign-Up: 03/02/02

Posts: 13,464

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

None

Idoru

Reply To Post Reply & Quote

Posted at: 3/16/05 10:42 PM

Idoru EVIL LEVEL 02

Sign-Up: 01/11/02

Posts: 689

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


None

Afro-Ninja

Reply To Post Reply & Quote

Posted at: 3/16/05 10:46 PM

Afro-Ninja EVIL LEVEL 37

Sign-Up: 03/02/02

Posts: 13,464

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

None

Idoru

Reply To Post Reply & Quote

Posted at: 3/16/05 10:57 PM

Idoru EVIL LEVEL 02

Sign-Up: 01/11/02

Posts: 689

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.


None

s1nt3ch

Reply To Post Reply & Quote

Posted at: 4/5/05 01:14 PM

s1nt3ch EVIL LEVEL 09

Sign-Up: 07/02/02

Posts: 3,612

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

Happy

BluArchon

Reply To Post Reply & Quote

Posted at: 6/4/05 10:37 AM

BluArchon LIGHT LEVEL 07

Sign-Up: 05/28/05

Posts: 35

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


Shouting

ApeLord

Reply To Post Reply & Quote

Posted at: 6/4/05 11:39 AM

ApeLord EVIL LEVEL 15

Sign-Up: 12/24/04

Posts: 1,559

Kool Denvish! I saw your flash tutorial on that aswell! Great Work!


None

fwe

Reply To Post Reply & Quote

Posted at: 6/7/05 01:12 AM

fwe DARK LEVEL 08

Sign-Up: 07/24/03

Posts: 3,361

Who didn't know this? besides noobs

wtfbbqhax


None

Toast

Reply To Post Reply & Quote

Posted at: 6/7/05 02:42 AM

Toast DARK LEVEL 09

Sign-Up: 04/02/05

Posts: 8,914

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!


None

Rustygames

Reply To Post Reply & Quote

Posted at: 6/7/05 04:18 AM

Rustygames LIGHT LEVEL 18

Sign-Up: 05/07/05

Posts: 6,662

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


None

Toast

Reply To Post Reply & Quote

Posted at: 6/7/05 11:26 AM

Toast DARK LEVEL 09

Sign-Up: 04/02/05

Posts: 8,914

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


None

Rustygames

Reply To Post Reply & Quote

Posted at: 6/7/05 12:53 PM

Rustygames LIGHT LEVEL 18

Sign-Up: 05/07/05

Posts: 6,662

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


None

Joelasticot

Reply To Post Reply & Quote

Posted at: 6/8/05 06:50 PM

Joelasticot EVIL LEVEL 37

Sign-Up: 02/14/03

Posts: 867

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

var s=0;var p=0;addEventListener("enterFrame", function(){
s+=(25-p)/20;p+=Math.ceil(s);var o="";
for(var i=0;i<p;i++){o+=" ";};trace(o+"joelasticot");})

BBS Signature

None

fwe

Reply To Post Reply & Quote

Posted at: 6/8/05 07:05 PM

fwe DARK LEVEL 08

Sign-Up: 07/24/03

Posts: 3,361

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


None

epheph

Reply To Post Reply & Quote

Posted at: 6/10/05 12:23 PM

epheph NEUTRAL LEVEL 12

Sign-Up: 03/31/05

Posts: 47

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


None

Hoeloe

Reply To Post Reply & Quote

Posted at: 7/4/05 11:42 AM

Hoeloe LIGHT LEVEL 28

Sign-Up: 04/29/04

Posts: 5,006

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!

Sex!
------------------------------
Super Nuke Bros. Melee, the web's no. 1 awaited Super Smash Tribute Game!

BBS Signature

All times are Eastern Standard Time (GMT -5) | Current Time: 12:50 AM

<< Back

This topic is 4 pages long. [ 1 | 2 | 3 | 4 ]

<< < > >>
You need a Grounds Gold Account to post on the NG BBS! If you don't have one, click here to sign up now! It's fast, free, and easy — and opens up tons of great NG features!