Results 1 to 5 of 5
Thread: Saving through cookies
-
March 9th, 2011, 12:26 PM #1
Saving through cookies
I would like some help with this. I am working on an RPG-esque game and would like to know how to save these variables
MaxHP = 100;
MaxMP = 55;
Gold = 100;
armorHelm = 1;
armorChest = 1;
armorLegs = 1;
weapon = 1;
I know i'm a little bit of a flash guru myself but this coding has been something that has always given me trouble.
I want to know how to save the data AND load it back with buttons.
Thanks in return[Insert Generic Signature Message Here]
-
March 9th, 2011, 03:21 PM #2
Take a look at the SharedObject class and this tutorial:
http://www.kirupa.com/developer/acti...ed_objects.htm
©2006-11 GlosRFC - Searching 8,168,684,336 brain cells
-
March 10th, 2011, 09:20 AM #3
so according this I would basically set the save up like this?
Am i getting this correct? that looks about right to me i just decided to put it in a function to call saving when I want to.Code:saveData1 = SharedObject.getLocal("player1Save"); saveGame = function(success){ if(success){ player1Save.data.maxhp = 100; player1Save.data.maxmp = 55; player1Save.data.level = 1; player1Save.data.gold = 100; player1Save.data.armorHelm = 1; player1Save.data.armorChest = 1; player1Save.data.armorLegs = 1; player1Save.data.weapon = 1; trace("Data Saved Successfully!"); } else { trace("Data could not be saved. Try enabling cookies on your browser.") } }Last edited by silverneo188; March 10th, 2011 at 09:54 AM.
[Insert Generic Signature Message Here]
-
March 10th, 2011, 11:18 AM #4
That's the gist of it, although the help docs do say the following:
"Do not assign values directly to the data property of a shared object, as in so.data = someValue; Flash ignores these assignments."
However, some tutorials imply that you can directly assign values! The safest method is to assign your values to some initial variables first, and then assign those variables to the data properties.
If you're planning on saving at regular intervals, you might want to include a SharedObject.flush() method call in your function to write the data to disk. Otherwise you're only updating the data properties until the SWF is closed, at which point the data is automatically written to the disk.
For example:
Then call the function whenever you want to write the data, say whenever a new level is started, or any of the values change.Code:MaxHP = 100; MaxMP = 55; Gold = 100; // etc saveData1 = SharedObject.getLocal("player1Save"); function saveGame() { player1Save.data.maxhp = MaxHP; player1Save.data.maxmp = MaxMP; player1Save.data.gold = Gold; // etc saveData1.flush(); }
Note that you might have to provide the scope to the variables, e.g. player1Save.data.maxhp = _root.MaxHP;
Retrieving the data, either automatically at the start of your SWF, or via a button event, is just a matter of reversing the statements:
When retrieving data, you should also check if the SharedObject and/or the data exists. The simplest way is to check if one of of your data properties is null:Code:function retrieveGame() { _root.MaxHP = player1Save.data.maxhp; // etc }
Code:if (player1Save.data.maxhp == null) { // Object doesn't exist so create it saveData1 = SharedObject.getLocal("player1Save"); player1Save.data.maxhp = MaxHP; } else { // Read data _root.MaxHP = player1Save.data.maxhp; // etc }
©2006-11 GlosRFC - Searching 8,168,684,336 brain cells
-
March 10th, 2011, 11:41 AM #5
Thank you so much GlosRFC.
I know about not applying values directly to the data portion hence the "player1Save.data.maxHP = maxHP;" etc...
But thank you so much for this coding help, for years I have had trouble understanding saving through sharedObjects but this completely helped me. I'm in class right now so I can't test it out yet but I will the moment I get home.
Also thanks for the extra tibit about checking for a save file so if I do save slots i could do something like this.
then the code on the save buttonPHP Code:onEnterFrame=function(){
//Save slot 1
if (player1Save.data.fileActive == null) {
statusText1.text = "No Save File Detected. Click 'Save' to create a save file"
} else {
statusText1.text = "Save File Present"
}
//Save Slot 2
if (player2Save.data.fileActive == null) {
statusText2.text = "No Save File Detected. Click 'Save' to create a save file"
} else {
statusText1.text = "Save File Present"
}
}
//Etc.......
obviously with the rest of the code for saving but just the basic for save file detection because I plan on doing save slots.Code://Save slot 1 button on(release){ saveData1 = SharedObject.getLocal("player1Save"); saveData1.data.fileActive = true saveData1.flush(); } //Save slot 2 button on(release){ saveData2 = SharedObject.getLocal("player2Save"); saveData2.data.fileActive = true saveData2.flush(); } //etc.....
To add I was thinking about using the flush property I just wasn't sure about using. But due to actually looking at what flush does I will be using a lot.
Once again thank you for all the help
________________________________________________
EDIT: oh question how would I also test for the fileActive variable to say "or"
like I will have a delete button for file slots so it will also set fileActive to false.
if (saveData1.data.fileActive == null or saveData1.data.fileActive == false);
or will this work
if (saveData1.data.fileActive == null | saveData1.data.fileActive == false);
____________________________________________
EDIT:EDIT: Nvrm i'll just use the clear command like this
that way saveData1.data.fileActive will once again read "null"Code:on(release){ saveData1.clear(); }Last edited by silverneo188; September 20th, 2011 at 02:48 PM.
[Insert Generic Signature Message Here]

Reply With Quote


Bookmarks