A Flash Developer Resource Site

Results 1 to 10 of 10

Thread: grrr...Array problems

  1. #1
    Tai'shar Manetheren! skierbit's Avatar
    Join Date
    Dec 2000
    Location
    Provo, Utah
    Posts
    407
    I cant get these variables in myArray to save correctly and then recall. Can you guys see anything wrong with it? I have tried hardcoding in a variable out of the myArray into both functions (ie sharedObject.data.myVar = myVar and visa versa and it will save/recall fine. It is when I try to save/recall all the variables via an array that it will not save correctly. Any ideas?

    Here is my code that uses the array's:
    Code:
    function saveMe() {
    	for (i=0; i < myArray.length; i++) {
    		sharedObject.data.myArray[ i ] = myArray[ i ];
    	}
    }
    function rememberMe() {
    	for (i=0; i < myArray.length; i++) {
    		if (sharedObject.data.myArray[ i ] != undefined) {
    			myArray[ i ]  = sharedObject.data.myArray[ i ];
    		}
    	}
    }
    sorry, it read my code as some HTML script and I didnt catch it
    [Edited by skierbit on 08-08-2002 at 08:10 PM]

  2. #2
    avatar free
    Join Date
    Jul 2002
    Location
    UK
    Posts
    835
    Your code it not all there me thinks - half of it is in italics because you probably have a [ i ] in there somewhere.

    Could you repost this code either using j or something instead of i? might be able to help a bit better then


  3. #3
    Tai'shar Manetheren! skierbit's Avatar
    Join Date
    Dec 2000
    Location
    Provo, Utah
    Posts
    407
    sorry about that.

  4. #4
    avatar free
    Join Date
    Jul 2002
    Location
    UK
    Posts
    835
    (GGGGGGr! i spent ages typing out your solution, and i forgot to put my name in didnt i... hit back button and post is gone!... !!!)

    Anyway, i'll start again...

    Your code is still half in italics , but i think i know where the last hiding [ i] should be...

    your code threw up an error in mx testing because you have your shared object named "sharedObject", and it doesn't like that one bit, so first rename your SO.

    Also, saving arrays... why loop through when you can save all at once! I only started learning about sharedObjects today and i found that you can simply save arrays using

    mySO.data.myArray = myArray;

    and vise versa, so if you want to pull out the whole array from the other end:

    myArray = mySO.data.myArray;

    Probably best to still keep some kind of check in there to see if anything comes out undefined etc...

    or if you awnt a specific item...

    ...data.myArray[itemnumber]; as usual

    Erm, what else was there? Nope, think that's all i put last time, try that and see. Good luck.


  5. #5
    Tai'shar Manetheren! skierbit's Avatar
    Join Date
    Dec 2000
    Location
    Provo, Utah
    Posts
    407
    Originally posted by jonmack
    [B}Anyway, i'll start again...

    Your code is still half in italics , but i think i know where the last hiding [ i] should be...

    your code threw up an error in mx testing because you have your shared object named "sharedObject", and it doesn't like that one bit, so first rename your SO.
    [/B]
    I am so sorry about the code not being completly posting, that was very stupid of me. I have tried what you have said and I get an undefined if I trace the array when it has variables in it, but when I have just text in the array it will trace back fine. Pretty much the variables that I am using are textField varibales that I want to be able to fill in again with the SO out of an array.

  6. #6
    avatar free
    Join Date
    Jul 2002
    Location
    UK
    Posts
    835

    Hey, just posting in case you're waiting for an answer..... i'll have to sleep on it i'm afraid. It's 2am for me and my brain just refuses to work anymore, but i want to know how to do this as well, so i will try again in the morning if noone else has got it...

  7. #7
    Tai'shar Manetheren! skierbit's Avatar
    Join Date
    Dec 2000
    Location
    Provo, Utah
    Posts
    407
    I tried the code below in a new MC and I came to this conclusion. I think that when the array is created it uses the values of the variables currently in use and creates a redundent copy of them where, for example, var1 is its original value not an updated variable as you change it. If you run this you will see what I mean. Anyone have confirmation on this? And if it is true, how do I create the array so that it truely can use the variables.


    var1, var2, and var3 are all variable names for input text fields.
    Code:
    myArray = [ var1, var2, var3 ];
    	mySO = sharedObject.getLocal("testObject6");
    function save(){
    	mySO.data.myArray_so = myArray;
    }
    function call() {
    	trace(var1);
    	trace(myArray);
    	trace(myArray[ 0 ])
    	trace(mySO.data.myArray_so.var1);
    	trace(mySO.data.myArray_so);
    	trace(mySO.data.myArray_so[ 0 ]);
    }

  8. #8
    avatar free
    Join Date
    Jul 2002
    Location
    UK
    Posts
    835

    Yeah, it's true that when arrays are created, they copy the variables current values, and keep them, even if the original variables are changed.

    The way around this is to save the variables into an array in the save() function, so they will have the most current variable values.

    So it will have to be something like this:

    function save()
    {
    myArray = [var1,var2,var3,...,varN];
    mySO.data.saveArray = myArray;
    }

    but if you have a long list of variables to save, then this is defeating the whole point of not having to type out loads of

    mySO.data.var1 = var1;
    mySO.data.var2 = var2;
    etc

    because we'll have to type them all out anyway to create the array...

    Is creating the array within the save function an acceptable solution to your problem? Because that;s the only way around it as far as i can see...

    P.S. When i tried your last code the 4th trace in call() came out undefined, so you can't load it like that, you have to use the array notation, like the last 2 trace elements.


  9. #9
    Tai'shar Manetheren! skierbit's Avatar
    Join Date
    Dec 2000
    Location
    Provo, Utah
    Posts
    407
    Thanks for all the help that you have given me, it has been invaluable. I can see that you have put much tim in to this problem and even more into the solution. Thank you. And yes, this does solve my problem!

  10. #10
    avatar free
    Join Date
    Jul 2002
    Location
    UK
    Posts
    835

    Glad to help. I wanted to learn about the new sharedObjects anyway, and the best way to learn is to get stuck in and experiment! Now i know a lot about SO's ,so we both get something out in the end . Thank YOU!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center