Forum Topic: AS2: vars in SharedObject?

(73 views • 7 replies)

This topic is 1 page long.

<< < > >>
None

simonhason

Reply To Post Reply & Quote

Posted at: 11/6/09 02:53 PM

simonhason LIGHT LEVEL 09

Sign-Up: 05/26/02

Posts: 311

Hey guys,

I am just starting to play with SharedOjbect to save my data, and haven't been able to find a good source on how to save variables with it ?? This is my script so far :

stop();
Stage.showMenu = false;
Stage.scaleMode = "noScale";
so = SharedObject.getLocal("player");
if(so.data.tname != undefined) {
	_root.headshots = 0;
	_root.kills = 0;
}else{
	_root.headshots = so.data.headshots;
	_root.kills = so.data.kills;
}

I have dynamic fields on the main stage with the variables : headshots / kills, but they are underfined with this so far. Also I had buttons that add to the variables for testing purposes but when I try adding it gives me NaN.

Any suggestions? Thanks


None

Rustygames

Reply To Post Reply & Quote

Posted at: 11/6/09 02:56 PM

Rustygames LIGHT LEVEL 18

Sign-Up: 05/07/05

Posts: 6,662

It's simple, just do the reverse. You can also call a function called flush which will make sure it saves it then and there:

so.data.tname
so.flush();

- Matt, Rustyarcade.com


None

Rustygames

Reply To Post Reply & Quote

Posted at: 11/6/09 02:57 PM

Rustygames LIGHT LEVEL 18

Sign-Up: 05/07/05

Posts: 6,662

Balls screwed that post up

var so:SharedObject = SharedObject.getLocal("myGame");
so.data.myVar = 100;
so.data.flush();

- Matt, Rustyarcade.com


None

K-Guare

Reply To Post Reply & Quote

Posted at: 11/6/09 03:01 PM

K-Guare FAB LEVEL 17

Sign-Up: 05/23/08

Posts: 2,413

At 11/6/09 02:53 PM, simonhason wrote: if(so.data.tname != undefined) {
_root.headshots = 0;
_root.kills = 0;

If so.data.tname is not undefined... set the textfields to 0?
That sounds like something you'd do if the SharedObject wasn't there yet,
but this code runs when it does find something for the SharedObject.
So I'm thinking you need to change that != to a ==.

Also, try tracing the SharedObjects and see what you get.
Because just because it doesn't show up in the textfield
doesn't mean it isn't there.

i work alone. i wore cologne.

BBS Signature

None

simonhason

Reply To Post Reply & Quote

Posted at: 11/6/09 03:03 PM

simonhason LIGHT LEVEL 09

Sign-Up: 05/26/02

Posts: 311

Ok I got the variables working right, but it doesn't seem to save... here is my code :

on _root frame :

so = SharedObject.getLocal("player");
if(so.data.tname != undefined) {
	_root.headshots = so.data.headshots;
	_root.kills = so.data.kills;
}else{
	_root.headshots = 0;
	_root.kills = 0;
}

on my save button :

on (press) {
	so.data.headshots = _root.headshots;
	so.data.kills = _root.kills;
	so.data.flush();
}

is this correct?

I have my sample up at :
http://www.simonhasondesign.com/SharedOb ject.swf


None

K-Guare

Reply To Post Reply & Quote

Posted at: 11/6/09 03:07 PM

K-Guare FAB LEVEL 17

Sign-Up: 05/23/08

Posts: 2,413

At 11/6/09 03:03 PM, simonhason wrote: is this correct?

Do you ever set a value for so.data.tname?
Because if you don't, there's your problem.

i work alone. i wore cologne.

BBS Signature

None

simonhason

Reply To Post Reply & Quote

Posted at: 11/6/09 03:13 PM

simonhason LIGHT LEVEL 09

Sign-Up: 05/26/02

Posts: 311

At 11/6/09 03:07 PM, K-Guare wrote:
At 11/6/09 03:03 PM, simonhason wrote: is this correct?
Do you ever set a value for so.data.tname?
Because if you don't, there's your problem.

Ah you're right, I got this from a tutorial ( not dealing with variables ) and I wasn't sure what tname was. I set a new variable called so.data.saved to be "true" when you press the save button, and on my frame

if(so.data.saved != undefined) {

which seems to be working.

One more question, if I have variables set to true and false how would I define those inside of the sharedObject?

ex :

if(so.data.saved != undefined) {
_root.unlocked = false;
}else{
_root.unlocked = so.data.unlocked;
}

and somewhere along the game _root.unlocked = true; so on the save would it be :

so.data.unlocked = _root.unlocked;
so.data.flush();

? Thanks so far you guys have been amazing help!


None

simonhason

Reply To Post Reply & Quote

Posted at: 11/6/09 03:24 PM

simonhason LIGHT LEVEL 09

Sign-Up: 05/26/02

Posts: 311

Shouldn't have been lazy and tested before I asked, but the answer to my above question is YES :D

Didn't think it would be that easy, what a relieve!

Thanks again all of you guys.


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

<< Back

This topic is 1 page long.

<< < > >>
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!