Save Game System AS2

Subscribe to Save Game System AS2 14 posts, 4 voices

Sign in to reply


 
avatar for Osiris602 Osiris602 81 posts
Flag Post

So yeah I’m making an RPG and it’s going along pretty good, with one major problem… I can’t figure out how to save it. I searched around and found this…

Object.prototype.setCookie = function(c, n, v) {
var so = SharedObject.getLocal©;
so.data[n] = v;
so.flush();
};
Object.prototype.getCookie = function(c, n) {
var so = SharedObject.getLocal©;
return so.data[n];
};

so then i have a button to set the cookie which has this code:
this.setCookie(“database”,“item1”,root.database)
and then the database
_root.database = new Array(
root.warrior, _root.totalHealth, _root.totalMana, _root.exp, _root.gold, _root.level, _root.guideGold, _root.shortSwordUnbought, _root.starterBladeEquip, _root.gotLettter);
and finally the get cookie code

_root.titleMenu.continueButton.onPress = function() {
_root.database = this.getCookie(“database”,“item1”)
gotoAndStop(4)
};
so i put all this together and….. nothing. Any help?

 
avatar for Wordblind Wordblind 119 posts
Flag Post

Perhaps you need to place the data back into the original variables:

_root.warrior = _root.database[0];
_root.totalHealth = _root.database[1];
// and so on...

Also, some objects may not be serialized and restored correctly without more logic.

 
avatar for Osiris602 Osiris602 81 posts
Flag Post

alright so something is wrong with the database? but do the actual create/get cookie functions look right?

 
avatar for samwarrior samwarrior 90 posts
Flag Post

I don’t know, your code looks insanely confusing to me..

But here’s the save-game system I use.

To start off:

saveslot = SharedObject.getLocal(“xxx”)

replace xxx with whatever you like; it will be the save file.

To Save

saveslot.data.xxx = yyy

replacing xxx with your saved variable name, and yyy with the actual variable

Say I wanted exp, then I would put:

saveslot.data.exp = exp;

Then you put

saveslot.flush();

to actually save it.

To Load

xxx = saveslot.data.yyy

replacing xxx with your variable and yyy with your saveslot variable

so exp = saveslot.data.exp

that’s all for loading.

Have fun :)

 
avatar for saybox saybox 140 posts
Flag Post

his code does the same thing except in a much more complicated way :P

the suggestion that you probably need to set them all back to variables is correct – youre loading an array of numbers, with nothing attached to them. you wont be able to call warrior until you’ve set it as in one of the previous replies.

if you’re not planning on saving more than that one array, then [n] is extra coding for nothing. there’s nothing in the code you posted that says that that need to be a variable at all; just make it a name and leave that out, unless you need it to be a variable (mlutiple save files would be one reason, except that you’re directly specifiyin the name anyway).

alternatively instead of saving the array all in one go, you could loop through it and save each as an increament of [n]. the benefit to this is that I assume you’re not actually using them as an array in the game itself, since they all have variable names. the other advantage of this would be that you wouldnt need to drag everything out of the saved array.

finally – d oyou really need to use such aroundabout way to save a few variables :P you could put everyhting you need to save in the setcookie function (sidenote: its incorrect to call them cookies, because they’re not :P )… i dont know how the rest of your game is written, or your level of scripting ability so perhaps youve chosen to do things differnt for a reson, but just from the code you’ve posted it seems unneeded :P

 
avatar for Osiris602 Osiris602 81 posts
Flag Post

alright guys thanks a bunch, saybox the reason there are so few variables is because i’m not even close to finishing the game yet :)

 
avatar for saybox saybox 140 posts
Flag Post

Yeah – but if the entire database will be in one array, then [n] is still not actuallyl doing anything, apart from complciateing the code. And if other variables will be saved outside the database array, then using the array seems pointless :P

 
avatar for Osiris602 Osiris602 81 posts
Flag Post

ok so i started just a thing to test this and put this code in the first frame

var nameTag1
var saveslot
_root.nameText.text = nameTag1
saveslot = SharedObject.getLocal(“saveslottest”)
_root.saveButton.onPress = function() {
saveslot.data.nameTag = nameTag1
saveslot.flush();
}
_root.loadButton.onPress = function() {
_root.nameTag = saveslot.data.nameTag
_root.nameText.text = nameTag1
}

so when i hit load it always comes up as undefined

 
avatar for saybox saybox 140 posts
Flag Post

Is the variable actualyl set anywhere befoer you save it? Perhaps i’m just being dumb (ithapepns a lot ), or maybe its set elsewhere in the script, but I dont’ seem to see it.

Anyway, assuming there’s data in trhe variable, the problem is just what you’re loading it back as: you have nameTag loading it, but then you’re trying to use nameTag1 in the text box, which doesn’t exist at that point.

general note: the line where you define saveslot should e outside of the save funtion and anything else, as you need it to load as well as to save :)

 
avatar for Osiris602 Osiris602 81 posts
Flag Post

ok lol i’m starting to get confused… so var nameTag1 and var saveslot are the only variables needing to be named right? and yeah that is the only coding in the file so far so its not named anywhere else this is just a test file

 
avatar for saybox saybox 140 posts
Flag Post
nameTag1 = "Cloud";
saveslot = SharedObject.getLocal(“saveslottest”);

_root.saveButton.onPress = function() {
saveslot.data.nameTag = nameTag1;
saveslot.flush();
}

you don’t sdeem to have set anything in the variable, so let’s call the character Cloud.

_root.loadButton.onPress = function() {
_root.nameTag1 = saveslot.data.nameTag;
_root.nameText.text = _root.nameTag1;
}

To check its working correctly – after saving / loading the first time, delete the line that originally sets “cloud”, then run the file again and try to load :)

 
avatar for Osiris602 Osiris602 81 posts
Flag Post

oooh lol your right i did forget to name the variable
/facepalm

 
avatar for Osiris602 Osiris602 81 posts
Flag Post

it works perfectly thanks tons

 
avatar for saybox saybox 140 posts
Flag Post

you’re welcome :D

Sign in to reply


Click Here