-
Truimagz.com
[RESOLVED] another sharedobject problem
Hi guys,
I'm re-visiting my old scripts and still having a problem with these shared objects.
I am trying to save some variables stored inside objects to a shared object.
Code:
//create new data objects
var SavedInfo : Object = new Object ( );
//add info
SavedInfo.SavedName = SavedName;
SavedInfo.textArray = curBtn.textArray;
SavedInfo.ButtonName = curBtn.Name
var o : Object = copy ( SavedInfo );
//set object into shared object
SavedData.data["Saved" + SaveSlot] = o;
SavedData.flush ( );
Code:
function copy ( obj : Object ) : Object {
var tempObj : Object = new Object();
tempObj.SavedName = obj.SavedName;
tempObj.textArray = obj.textArray;
tempObj.ButtonName = obj.ButtonName;
return tempObj;
};
In my eyes I would think this would work.
BUT!!!! for some reason even after adding in this copy function, if I change anything inside the
curBtn.textArray
the shared objects value also changes!!!
I thought that variables created inside functions got deleted after the function call? So why then is my shard object changing when I change
curBtn.textArray
please help! I'm totally lost!
Last edited by everfornever; 04-09-2009 at 06:22 PM.
-
Pencil Farmer
Because you're just passing around a reference to the same array.
Someone asked this same thing a few days ago:
http://board.flashkit.com/board/showthread.php?t=792644
You can use concat() as a quick way to copy an array:
PHP Code:
tempObj.textArray = obj.textArray.concat();
-
Truimagz.com
yea i saw that thread and really thought his solution would help me...
but when I add
PHP Code:
Object.prototype.copy = function() {
var tempObj = new Object();
for (var i in this) {
tempObj[i] = this[i];
}
return tempObj;
};
to my script, and not even calling it, it causes alot of problems in my app, just simply pasting that code block in my script causes half my app to fall apart.
I will try
PHP Code:
tempObj.textArray = obj.textArray.concat();
and see if that works.
-
Truimagz.com
JEEZ!
Still not working!
PHP Code:
//create new data objects
var SavedInfo : Object = new Object ( );
//add info
SavedInfo.SavedName = SavedName;
SavedInfo.textArray = curBtn.textArray;
SavedInfo.ButtonName = curBtn.Name
var o : Object = copy ( SavedInfo );
//set object into shared object
SavedData.data["Saved" + SaveSlot] = o;
SavedData.flush ( );
PHP Code:
function copy ( obj : Object ) : Object {
var tempObj : Object = new Object();
tempObj.SavedName = obj.SavedName.toString();
tempObj.textArray = obj.textArray.concat();
tempObj.ButtonName = obj.ButtonName.toString();
return tempObj;
};
When I change
curBtn.textArray
It's still chnaging the shared object
-
Pencil Farmer
Hmm.
Something else must be screwing you up then.
I tried this all by itself:
PHP Code:
var SavedData:SharedObject = SharedObject.getLocal("arrayTest");
//create new data objects
var SavedInfo : Object = new Object ( );
//add info
var myArray:Array = new Array(1,2,3);
SavedInfo.textArray = myArray;
var o : Object = copy ( SavedInfo );
//set object into shared object
SavedData.data.myObject = o;
SavedData.flush ( );
function copy ( obj : Object ) : Object {
var tempObj : Object = new Object();
tempObj.textArray = obj.textArray.concat();
return tempObj;
};
myArray = new Array(4, 5, 6);
trace(SavedData.data.myObject.textArray); // traces [1,2,3]
It worked just fine.
Maybe take a look at the part of your code that reads the data back in. It could be getting overwritten there. ?
-
Truimagz.com
sorry thought it would work.... but its not
your no doing what I am doing.
I have objects inside my array.
Below I took your script and made a very minor change to show you how I am storing objects inside my array.
Changing the array is not the problem, the problem is changing the obj values inside the array.
look below
Last edited by everfornever; 04-09-2009 at 10:43 PM.
-
Truimagz.com
Ok, I took your sample script and laid it out exactly how I am doing this, but using your script, I simply edited to follow the steps I am taking.
Code:
var SavedData : SharedObject = SharedObject.getLocal("arrayTest");
//add info
var myArray : Array = new Array ( );
var obj : Object = new Object ( );
obj.one = "one";
obj.two = "two";
myArray.push ( obj );
//create new data objects
var SavedInfo : Object = new Object ( );
SavedInfo.myArray = myArray;
function copy ( obj : Object ) : Object {
var tempObj : Object = new Object();
tempObj.myArray = obj.myArray.concat();
return tempObj;
};
var o : Object = copy ( SavedInfo );
//set object into shared object
SavedData.data.myObject = o;
SavedData.flush ( );
trace(SavedData.data.myObject.myArray[0].one); // traces "one"
obj.one = "changed"
trace(SavedData.data.myObject.myArray[0].one); // traces "changed", because the shard objects values are changing
Last edited by everfornever; 04-09-2009 at 09:25 PM.
-
Truimagz.com
You'll see above that using your exact same script.
but changing any of the obj varaibles also changes the shard object.
So it's still not working.
man this is so frustrating
Last edited by everfornever; 04-09-2009 at 09:37 PM.
-
Truimagz.com
I just don't understand.
We created two seperate and brand new objects, one of which occurs inside a function.
Then even used .concat on one of the arrays.
And then changing the original obj value still changs the SharedObject
trace(SavedData.data.myObject.myArray[0].one); // traces "one"
obj.one = "changed"
trace(SavedData.data.myObject.myArray[0].one); // traces "changed"
Someone has to know how to break this connection so I can edit
obj.one
Without it changing my sharedobject value
var SavedData : SharedObject = SharedObject.getLocal("arrayTest");
//add info
var myArray : Array = new Array ( );
var obj : Object = new Object ( );
obj.one = "one";
obj.two = "two";
myArray.push ( obj );
//create new data objects
var SavedInfo : Object = new Object ( );
SavedInfo.myArray = myArray;
function copy ( obj : Object ) : Object {
var tempObj : Object = new Object();
tempObj.myArray = obj.myArray.concat();
return tempObj;
};
var o : Object = copy ( SavedInfo );
//set object into shared object
SavedData.data.myObject = o;
SavedData.flush ( );
trace(SavedData.data.myObject.myArray[0].one); // traces "one"
obj.one = "changed"
trace(SavedData.data.myObject.myArray[0].one); // traces "changed"
what is it that I am missing?
How the heck can this sharedobject remmember all those steps and change itself when I change
obj.one
Last edited by everfornever; 04-09-2009 at 09:40 PM.
-
Truimagz.com
Ok this is realy getting crazy...
I even added a for loop and a push to create a new array, not an =.
look
PHP Code:
function copy ( obj : Object ) : Object {
var tempObj : Object = new Object();
var tempArray : Array = new Array();
tempObj.myArray = tempArray;
for ( var i : Number = 0; i < obj.myArray.length; i ++ ) {
tempObj.myArray.push ( obj.myArray[i] );
};
return tempObj;
};
Even when I add that into my copy function it still doesnt work right.
I can still change obj.one value and the shared object updates.
PHP Code:
var SavedData : SharedObject = SharedObject.getLocal("arrayTest");
//add info
var myArray : Array = new Array ( );
var obj : Object = new Object ( );
obj.one = "one";
obj.two = "two";
myArray.push ( obj );
//create new data objects
var SavedInfo : Object = new Object ( );
SavedInfo.myArray = myArray;
function copy ( obj : Object ) : Object {
var tempObj : Object = new Object();
var tempArray : Array = new Array();
tempObj.myArray = tempArray;
for ( var i : Number = 0; i < obj.myArray.length; i ++ ) {
tempObj.myArray.push ( obj.myArray[i] );
};
return tempObj;
};
var o : Object = copy ( SavedInfo );
//set object into shared object
SavedData.data.myObject = o;
SavedData.flush ( );
trace(SavedData.data.myObject.myArray[0].one); // traces "one"
obj.one = "changed"
trace(SavedData.data.myObject.myArray[0].one); // traces "changed"
Now how in the world is this even possible... how is the shared object remmembering all of those steps?
-
Objects are passed by reference just like arrays. You can't just push your original object into a new array to create a copy. You need to do a deep copy, something like this:
Code:
function copy (o:Object):Object {
var co:Object = null;
co = new Object();
for (var k:String in o) {
if (o[k] is Object) {
co[k] = copy(o[k]);
} else {
co[k] = o[k];
}
}
return co;
};
Warning: The code is untested but that is the general idea.
-
Pencil Farmer
Well, you're doing a good job of making sure you're using a unique ARRAY, but the OBJECTS you're pushing into it are still just references to the same object.
I don't use generic objects much, but I'm sure you could google around for a method to clone objects.
-
Pencil Farmer
Ah, Stephen beat me to it. With a code sample to boot.
-
Truimagz.com
Still not working
I added your new code to my copy function too...
PHP Code:
function copy ( o : Object ) : Object {
var co : Object = null;
co = new Object ( );
for ( var k : String in o ) {
if ( o[k] == Object ) {
co[k] = copy ( o[k] );
} else {
co[k] = o[k];
}
}
return co;
};
var SavedData : SharedObject = SharedObject.getLocal("arrayTest");
//add info
var myArray : Array = new Array ( );
var obj : Object = new Object ( );
obj.one = "one";
obj.two = "two";
myArray.push ( obj );
//create new data objects
var SavedInfo : Object = new Object ( );
SavedInfo.myArray = myArray;
//set object into shared object
SavedData.data.myObject = copy ( SavedInfo );
SavedData.flush ( );
trace(SavedData.data.myObject.myArray[0].one); // traces "one"
obj.one = "new";
trace(SavedData.data.myObject.myArray[0].one); // traces "changed"
I do something wrong?
-
Yeah, o[k] == Object won't check the type of a variable.
typeof o[k] == "object"
try that, i forgot all classes extend Object.
-
Truimagz.com
Stephen,
I believe that did it.
Let me go add it into my actuall application and see what happens.
Thank you for joining in and helping.
Thank you as well cadin
-
Pencil Farmer
Google is your friend.
I found this pretty quickly.
It works for me:
PHP Code:
function copy(source:Object):* {
var copier:ByteArray = new ByteArray();
copier.writeObject(source);
copier.position = 0;
return(copier.readObject());
}
var SavedData : SharedObject = SharedObject.getLocal("arrayTest");
//add info
var myArray : Array = new Array ( );
var obj : Object = new Object ( );
obj.one = "one";
obj.two = "two";
myArray.push ( obj );
//create new data objects
var SavedInfo : Object = new Object ( );
SavedInfo.myArray = myArray;
//set object into shared object
SavedData.data.myObject = copy ( SavedInfo );
SavedData.flush ( );
trace(SavedData.data.myObject.myArray[0].one); // traces "one"
obj.one = "new";
trace(SavedData.data.myObject.myArray[0].one); // traces "one"
-
Truimagz.com
Hey,
It's working great HOWEVER, again another issue.
Like I said though it's working great and I thank you for your help first.
But the issue is this, I can trace the values of my objects inside my array by doing
array[0].txt
or
array[1].txt
And like I said it traces those perfect, But when I do
trace ( array.length )
it returns undefined... Any ideas?
(it's not a huge issue, I just simply changed my for loop that uses testarray.length, to a while loop that does while testarray[i] and it works out to be ok)
Cadin,
I use google alot, and I'm usually very good about finidng my own solutions, just this time I didn't really know what to look for, if you know what I mean
Last edited by everfornever; 04-09-2009 at 11:39 PM.
-
Truimagz.com
Just wanted to say thanks again.
I know I can get frustrated easily, thank you for helping me.
you guys have been great, and you have solved my problem, I hope you get some kind of happy feeling inside knowing you helped someone, cuz you really did help me, both of you guys.
Thanks again
-
Pencil Farmer
Yeah, I was just giving you a hard time.
I know you're a smart guy and could have found a solution on your own once you found out what the problem was, so I was just razzing you a bit.
Glad you got it all worked out.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|