PDA

View Full Version : Saving Tutorial (includes sample file)


The First
06-16-2003, 11:00 AM
How do you make your game savable? :)

skye
06-20-2003, 02:51 AM
Hello! Sorry I took so long to get around to this question.

The answer lies in the new sharedObject data type introduced in Flash MX. (Versions of Flash prior to MX will not work with this method)

I prefer to split my Saving and Loading code into functions in order to keep things neat and organized.

Place this code in the first frame:

function saveGame(){
myLSO = SharedObject.getLocal("uniqueName");

if(myLSO.data.myObj == undefined){ // No object exists
trace("Saved Game");
}
else{
trace("Overwrote Saved Game");
}
myObj = {};
myObj.objArray = new Array();

myObj.objArray[0] = lives;
myObj.objArray[1] = score;
myObj.objArray[2] = apples;

myLSO.data.myObj = myObj;

}


function loadGame(){
myLSO = SharedObject.getLocal("uniqueName");
if(myLSO.data.myObj == undefined){ // No object exists
trace("No Saved Game");
}
else{
trace("Loaded Game");

lives = myLSO.data.myObj.objArray[0];
score = myLSO.data.myObj.objArray[1];
apples = myLSO.data.myObj.objArray[2];


}

}
That's it!

Now you can just call the save and load functions like this:

saveGame(); // Code to save current state
loadGame(); // Code to load current state


There are a few things you want to change in the code itself to customize it for your specific needs.

- Replace "uniqueName" with... yeah... a unique name :)
This will identify which object to save/load, so that you can have multiple objects stored on the users HDD.

- Change the variables. 'Lives' and 'Score' are probably different than the variable names you want to save. You should include all variables which are important to the game state, and you can include more than three by using more array locations.

EG.
myObj.objArray[3] = oranges;
myObj.objArray[3] = coconuts;
etc....

*UPDATE 01.28.04*

An example FLA file is now attached.

Enjoy!

The First
06-20-2003, 11:58 PM
Thank you Skye :) That helps a lot. #^_^#

thohan
08-07-2003, 11:17 AM
Just another example of why Flash MX is so cool.

jason
08-07-2003, 01:20 PM
well obviously it is cool lol i just got started and made a few cool things

chriso20
12-14-2003, 04:52 PM
hey!

cool coding there, but how can i;

1) let people save a game on PC1 and replay/resume on PC2 ? (i believe you can't do this with sharedObject/cookies.)

OR

2) make a database to save people's variables + scores etc.. ?

ANY help Pleaase! :) thnx

illrainman
12-14-2003, 05:00 PM
The PC1 save loading on PC2 would possibly work but would require searching the other comp through the internet, requiring both to be on and connected through a lan I think. Not too sure about that.

I think the database thing would require screennames and such. I have no idea about that.

P.S. Nice avator.

chriso20
12-14-2003, 05:03 PM
yeah if someone could tell me a way of saving a text file from flash containing variables then i can sort it from there.

thanks chris :)

ps. i'm a \/\/ /\ B B | T

The Brown Cow
12-14-2003, 05:37 PM
To save on one computer and load on another, you'd probably want to save the file to the server. It would take way too much writing for me to try to explain it (poorly) here, so check out anything you can find about XML in Flash.

chriso20
12-14-2003, 05:38 PM
i'd prefer to use php + mySQL

denacioust
12-29-2003, 06:11 PM
Wow thanks Skye!

The First
12-30-2003, 04:19 AM
wow, that was random. But I'm assuming PHP and MySQL work the same way in Flash as in HTML/Websites, but I have no clue, to be honest. But your PC1 and PC2 problem would be solved with a database, assuming both computers had access to the internet :p

Darc^2
12-30-2003, 11:08 AM
well i think darkreality has a point as does eveyone else. i do believe any flash game that has the ability to keep score/ or save has to be ran off a php,mySQL database. whether it be through cookies or usernames. i thinkn usernames is cooler because is kinda gives that personal effect to the game.

later

The Brown Cow
12-30-2003, 12:27 PM
You can save with cookies without using php or mySQL. The cookie is saved only to the user's computer. Usernames, like in Cinderfall, would need a database on the server.

Nexus
02-08-2004, 07:27 PM
Hey, I'm running XP, and whenever I delete my cookies and my Temporary Internet files right after saving a Defend Your Castle game, I can still load the game.

Basically, my question is, what is the directory in which the cookies are saved for a XP user???

Sli
04-09-2004, 08:22 AM
This is the same thing that happens to me, but I'm using windows ME. I just deleted all of my Temps(I think) yet I can still play DYC without conflict. They must be saved in a different directory maybe...........

The First
04-09-2004, 09:45 AM
I can't help the ME user, but the saved game for Castle is under:

C:\Documents and Settings\default\Application Data\Macromedia\Flash Player\xgenstudios.com and so on, you get the point, it shouldn't be hard to find from there :-) You might have to turn on hidden folders though.

(the folder names may not be correct, I simlpy used a dictionary to translate, I figured the real names will at least be synonyms. You're smart, you'll find 'em).

Basically, your savegame isn't saved in your tempfiles, cookies or anywhere it used to be saved. I'm assuming it's similar to ME...

sn1per
04-16-2004, 10:48 AM
WOW Skye! You're really a master to do flash games :wink: Hehe, your tutorial was really helpfully to make my game Skye! Thanks!

thE_GINGERBREAD_Man
06-03-2004, 01:17 AM
i would check out webmonkey's tutorial on how to incoorperate php in flash

www.webmonkey.com

hope it helped someone ;)

tatorhead
06-09-2004, 02:43 PM
I have a question involving saving flash cookies. If say I had a game on my website and a player saved the gave using cookies or whatnot, and then I updated it, would the player still be able to load their settings as long as the variables were the same?

The Brown Cow
06-10-2004, 09:11 AM
If the file has the same name, then yes, it should still work.

Swas
12-11-2004, 05:29 AM
im starting to understand savings... but is there a way u can save in one flash movie and enter it in another is it php or xml or some annoying abbreviation ?

The Brown Cow
12-11-2004, 11:39 AM
You mean on a nother computer, like the XGen games?

The easiest way by far is to store the data in a mySQL database on your server. Then you use PHP to communicate with the server.

The basic flow is...
-Flash sends data to a PHP file.
-PHP file places data in server and gets result.
-PHP prints results out.
-Flash loads in results.

So you kinda need to know PHP (or ASP, Perl, etc.) to pull it off. You should probably know SQL too, but it's not at all hard to use (single lines of code usually, very literal).

Swas
12-12-2004, 04:45 AM
thanx anyway, but its not really what i meant , i meant can you have one flash movie use data and then say a sequel use it as well, because the game im thinking about would be better realesed in episodes, ill have to get a server i spose :roll: would it pretty much be the same kind of method?

The Brown Cow
12-12-2004, 09:15 AM
Well, you can use shared objects, which are stored on the user's harddrive. Multiple files can load from and write to the same shared object, so that's probably what you're looking for.

Nexus
12-12-2004, 02:27 PM
If you want it so that the user can access their saved file from any computer, you need php/mySQL. This will also work for subsequent "episodes" of your game as long as you design it correctly.

If you just want to have it save the game on the user's local hard drive, use shared objects. The saving tutorial in this topic uses shared objects. PHP and mySQL are complicated in that you need to have a good knowledge in designing effective database applications.

If you want to have a saving system like Motherload, go with the PHP/mySQL. Otherwise, if you're just making something small like Defend Your Castle, go with shared objects.

Swas
12-13-2004, 10:40 AM
i have used a shared objects , would i basically use the same code i used to retrieve the data on the loading function? in the other game as long as the variable there were loading were named the same and in the same place?

The Brown Cow
12-13-2004, 01:47 PM
Yes. The getLocal() method of the SharedObject class has an optional path arguement.

my_so.getLocal(objectname, local_path);


To quote Flash's help document on getLocal()...
localPath An optional string parameter that specifies the full or partial path to the SWF file that created the shared object, and that determines where the shared object will be stored locally. The default value is the full path.

So if you specify the same object and location for both files, you'll be loading the same file.

ED!!!
01-16-2005, 10:37 AM
Hey there, can you please make an example of what I should set in the local_path and objectname....!

In local_path you set : www.something.something/something isnt it?

userdan
01-16-2005, 10:49 AM
Where the F*uck is the *.fla file you said is attached?

The Brown Cow
01-16-2005, 11:15 AM
1. Swearing will get you banned. Adding a * into the word still counts.

2. The attachment MOD was removed from the board, so the file is no longer accessable. You can ask Skye for a link though.

VENGEANCE
01-17-2005, 04:02 PM
thanx anyway, but its not really what i meant , i meant can you have one flash movie use data and then say a sequel use it as well, because the game im thinking about would be better realesed in episodes, ill have to get a server i spose :roll: would it pretty much be the same kind of method?

As long as the sequels to the game are all inside the same individual file (you could update it whenever you wanted), it should still retain the data. However, the best thing to do would be to make one main file that all the data is saved to, and it would dynamically load either of the episodes of the game into itself at the click of a button. That way, you'd be able to use the same save file for multiple games.

The Brown Cow
01-17-2005, 05:41 PM
Two Flash movies can use the same shared object as long as the SharedObjects' name and local_path are identical. You're really just loading the same file that way.

ED!!!
01-20-2005, 08:52 AM
But I don't understand, what should I set in the local_path and name!

I think that in local_path it's a domain name isn't it?

The Brown Cow
01-20-2005, 11:02 AM
local_path is the directory where you want to store the data on the user's computer. You can just leave it blank if you want.

// Create a SharedObject to save in
saver = SharedObject.getLocal("player1data");
// Store my name
saver.data.name = "The Brown Cow";
// Write the data to disk
saver.flush();

ED!!!
01-21-2005, 02:06 PM
So that is the load action... I have a shared object saving script with roots_ so that work with your load actionscript!?

The Brown Cow
01-21-2005, 06:19 PM
I believe that if the files are on the same website, the code required to load/create a SharedObject for both of them uses the local_path "/".

saver = SharedObject.getLocal("mydata", "/");

HPen
01-22-2005, 06:26 AM
how is the save action and load action in the main file and how is the action from they other files?

my cookie names "hej" without "

and my dynamic text box name is euro...

and my website is www.haldpower.dk

what is the actions? :D

come with an example thanks :wink:

HP

The Brown Cow
01-23-2005, 08:37 PM
If you want to save a SharedObject named hej with the string stored in a textbox name euro, you could use this code...

mydata_so = SharedObject.getLocal("hej");
mydata_so.data.textfieldcontents = _root.euro.text;
mydata_so.flush();

HPen
01-24-2005, 06:24 AM
but will it works in many files? :D

HP

PS: thank you for helping us :wink: it is the only thing we must do to finish our rpg.....

The Brown Cow
01-24-2005, 06:57 AM
To save and load in multiple files, I think you can use this code in each file...

mydata_so = SharedObject.getLocal("hej", "/");
// Ignore from here on unless you want to save that text
mydata_so.data.textfieldcontents = _root.euro.text;
mydata_so.flush();

HPen
01-24-2005, 08:14 AM
is that action to save AND load action?
:)

so i can take that action and place it in both files a save button and load button?

The Brown Cow
01-24-2005, 04:27 PM
The first line loads the file.
The last line saves the file.

ED!!!
01-25-2005, 09:27 AM
THANKS A LOT!

HPen
01-25-2005, 09:52 AM
its that all?? :D

its that the only action we gonna need?

it sounds very easy

i mean should you not write something about shared object in the save action?

EDIT: it dont work with me....... have i do something wrong?

The Brown Cow
01-25-2005, 03:33 PM
Okay, here...

// This is the SAVE code
// Let's pretend we want to save the player's name: Billy
saver = SharedObject.getLocal("hej", "/");
saver.data.name = "Billy";
saver.flush();

// This is the LOAD code
loader = SharedObject.getLocal("hej", "/");
// Display the player's name in the output window
trace(loader.data.name);

ED!!!
01-26-2005, 08:27 AM
Thanks again!

But... The problem is that the script you gave me can save "Billy". We need to have a script that can save for example the money the character got.
Like : The guy knocked out many enemies and have saved lots of money... So can you tell me how to save a dynamic text with a random number in it! And that the variable is "euro"

VENGEANCE
01-26-2005, 12:54 PM
Thanks again!

But... The problem is that the script you gave me can save "Billy". We need to have a script that can save for example the money the character got.
Like : The guy knocked out many enemies and have saved lots of money... So can you tell me how to save a dynamic text with a random number in it! And that the variable is "euro"x = /*the maximum number of dollars you want to give out*./;
_root.euro = random(x)+1;

And seriously... it's not that hard to make it save something other than the character's name. have you even tried tinkering with it yet?

The Brown Cow
01-26-2005, 03:27 PM
Seriously, play with it a little. Anything stored as a property of saver.data will be written to disk.

// This is the SAVE code
saver = SharedObject.getLocal("hej", "/");
saver.data.name = "Bob";
saver.data.money = 500;
saver.data.hasCar = false;
saver.data.textfieldContents = _root.mytextfield.text;
saver.flush();

In this example, name, money and hasCar are all assigned literal values. textfieldContents, however, stores whatever text you have in a textfield named mytextfield. To save a variable, just set a property of saver.data equal to it.

var myvariable = 5;
saver.data.num = myvariable;

HPen
01-27-2005, 09:34 AM
Thanks again!

But... The problem is that the script you gave me can save "Billy". We need to have a script that can save for example the money the character got.
Like : The guy knocked out many enemies and have saved lots of money... So can you tell me how to save a dynamic text with a random number in it! And that the variable is "euro"x = /*the maximum number of dollars you want to give out*./;
_root.euro = random(x)+1;

And seriously... it's not that hard to make it save something other than the character's name. have you even tried tinkering with it yet?

yes ok.......... your actionscript and the brown cows is not the same so what is your action to save and loade? you know that action you whrite before: "root: euro = random (x) +1 :wink:

The Brown Cow
01-27-2005, 01:16 PM
Okay, I'll break down the code for you. Hopefully by understanding what it does, it will be easier for you to make your program.

First, the saving code:

saver = SharedObject.getLocal("hej", "/");
This creates a new object in Flash, named saver. saver is an instance of a SharedObject - a class built into Flash. SharedObjects are used to write data to a users disk, much like a cookie.
The SharedObject is known as saver within Flash, but the name of the actual file to create is "hej", as specified within the parenthesis. The "/" says where the file should be saved - in this case in a common directory (so two files can access it).

saver.data.name = "Bob";
Your SharedObject saver has a property called data. This is a list of values that you want stored within the saved file. To access a property of an object, you use dot syntax:
<object>.<property>
Ex: _root.myMovieClip

So, we create a property within data called name. We then assign it the value of "Bob".

saver.flush();
Every instance of a SharedObject has a method (a command that can be run on it) called flush(). Flushing a SharedObject is what actually saves the file to disk. Very important. If we don't flush the SharedObject, we don't actually save anything.


Alright, now to load...

loader = SharedObject.getLocal("hej", "/");
We make a new SharedObject, this time named loader. The name doesn't affect how it runs, it's just a way for us to know what it does. Notice that the name of the file and the directory are the same as in the saving code. This way we can access the same file.

So, now we've actually done all we need in order to load the file. But we probably want to actually do something with that iinformation. For this simple example, let's just display the name we saved before...

trace(loader.data.name);
Again we access the properties using dot syntax. We stored some information in a property called name of the saver's data, so we can load it from the loader's data.




I don't think I can break it down any further without going into the very basics of programming.

HPen
01-28-2005, 05:55 AM
ok.......... so if i whrite saver.data.euro i will load the dynamic text box always....... an exampel: you have won a figth and get 100 euro and then it will be save and then you fight against other monsters and get lots of euros and your action will also save that and when you make another flash file and whrite the load action down your dynamic euro textbox will be load.... :D


you are a genius :o

ED!!!
01-29-2005, 07:30 AM
Vengeance can you please make the script with the maximum dollar. :D

realplaya776
02-04-2005, 09:35 PM
thanks

HPen
02-21-2005, 11:33 AM
yes,Vengeance can you please make the script with the maximum dollar.

flashgamer
04-09-2005, 06:13 AM
Is there a way to save the whole frame instead of just the variables?

Swas
04-09-2005, 08:54 AM
im guessing not, if i understand what you mean, you could save what frame they were on but not the whole frame coding

Boshinator24
04-10-2005, 12:45 PM
How do you save what frame they got to?

moonm325
04-10-2005, 01:12 PM
Just have a global, and on each frame:
onClipEvent(load){
frameCount++;
}

arkhan
04-10-2005, 03:11 PM
_root.currentFrame is the current frame from the stage..

skyrider123
07-26-2005, 03:56 AM
I understand the code and all but i need and example file to know how to integrate it into my movie :mad: !!

I but the code in a button so when a release event happens it executes the save code and then when i test out the movie no file appears i am realy confused!! :confused: please someone send me an email skyrider123@gmail.com with the *.fla attached i realy need help. evan if it just saves a file to desktop with a few values in it...

Mod#4
07-26-2005, 07:14 AM
I understand the code and all but i need and example file to know how to integrate it into my movie :mad: !!

I but the code in a button so when a release event happens it executes the save code and then when i test out the movie no file appears i am realy confused!! :confused: please someone send me an email skyrider123@gmail.com with the *.fla attached i realy need help. evan if it just saves a file to desktop with a few values in it...

Don't bump old threads you tard!

daperson
10-01-2005, 08:35 AM
hmm is this AS2?

The Brown Cow
10-01-2005, 10:49 AM
No, AS 2.0 is only written in external files, and tends use words like "class" and "extends" frequently.
If you're typing code into the Actions panel in any version of Flash, then you need to be using AS 1.0. I don't know where people are getting the idea that Flash MX uses AS 2.0 in the Actions panel.

denacioust
10-01-2005, 11:48 AM
Don't bump old threads you tard!

Don't insult people you tard!

killall 001
10-28-2005, 11:40 AM
Hey, I'm running XP, and whenever I delete my cookies and my Temporary Internet files right after saving a Defend Your Castle game, I can still load the game.

Basically, my question is, what is the directory in which the cookies are saved for a XP user???

The defend your castle game saves use .sol files if you want lot's of archers, wisard,craftmen or what ever PM me

Steven
12-21-2005, 02:07 PM
I have used this code in my game, it doesn't work and I can't find any problem with it.

//Save and Load game functions
function saveGame(){
myLSO = SharedObject.getLocal("uniqueName");

if(myLSO.data.myObj == undefined){ // No object exists
trace("Saved Game");
}
else{
trace("Overwrote Saved Game");
}
myObj = {};
myObj.objArray = new Array();

myObj.objArray[0] = raceforsave;
myObj.objArray[1] = wood;
myObj.objArray[2] = gold;

myLSO.data.myObj = myObj;

}


function loadGame(){
myLSO = SharedObject.getLocal("uniqueName");
if(myLSO.data.myObj == undefined){ // No object exists
trace("No Saved Game");
}
else{
trace("Loaded Game");

raceforsave = myLSO.data.myObj.objArray[0];
wood = myLSO.data.myObj.objArray[1];
gold = myLSO.data.myObj.objArray[2];


}

}

Matt
12-21-2005, 02:14 PM
did you put that on the frame and make a button to save with this on it
on(release) {
saveGame();
}
and a load one with
on(release) {
loadGame();
}

Steven
12-22-2005, 04:53 AM
I put:

on(release) {
saveGame();
gotoAndStop("Game",1);
}

and on the "Game" frame I put:
if (_root.raceforsave == "no save")
{gotoAndStop(5);}

and every time it goes to frame 5.

hpr
12-28-2005, 10:44 AM
i have only 1 thing to say....

stickarena is made very good. you must be a genius to make it :p

ellit
01-23-2006, 08:50 AM
where do you get flash xm from

Shintaja
01-23-2006, 10:13 AM
i have only 1 thing to say....

stickarena is made very good. you must be a genius to make it :p
not realy its quit simple to make if u know how to work with flash servers

MOOGLEFIGHTER
02-12-2006, 05:36 PM
That may have been the most helpful thing I have seen for a while. The people behind XGen Studios rock!

LizardRob
04-03-2006, 12:14 PM
Where is the sample .fla gone? Was it deleted? I do not understand it and I'd like to know how it works. Thanks.

mayjarboi
04-23-2006, 10:49 AM
sorry to bother you but where it that fla you posted

mayjarboi
04-23-2006, 10:59 AM
neva mind i got it to work

Soon to be flash master
05-06-2006, 07:35 PM
hmm 2 posts on teh forums, and its a double post

curtisr
05-20-2006, 07:55 PM
Thank You!!!!! :D

Mechanized
08-11-2006, 02:25 AM
Where can I DL The sample file?

Soon to be flash master
08-11-2006, 02:56 AM
not realy its quit simple to make if u know how to work with flash servers

No it isnt. its mind bogglingly complicated.

Blackegg-Clock
08-29-2006, 02:20 PM
wow that helped a lot thanks :)

rpgfan
10-10-2006, 07:45 PM
i'd prefer to use php + mySQL
Why must everyone do it the hard way?

phdrummer
12-17-2006, 09:18 AM
im suprised this sticky isn't locked yet, i mean arent stickies usually?

Mark Aged
01-05-2007, 10:52 AM
I was thinking, will this work with Flash 8? And, the "uniqueName" is the name of what the file will be called, right?

cooldude94
01-12-2007, 06:09 PM
I was thinking, will this work with Flash 8? And, the "uniqueName" is the name of what the file will be called, right?

Yes and Yes.

Mark Aged
01-20-2007, 12:40 PM
Oh, okay. Thanks.

flash_gamer
07-30-2008, 07:56 AM
thank for the tuturial

F2razor
12-25-2008, 12:24 PM
thanx anyway, but its not really what i meant , i meant can you have one flash movie use data and then say a sequel use it as well, because the game im thinking about would be better realesed in episodes, ill have to get a server i spose :roll: would it pretty much be the same kind of method?
yea i think you would get all your games just to use one joined database