0

I'm having a problem with the sharedObject flush method. Is it possible to not flush the new data when the swf is closed? My savegame function is the only function that calls the flush method and it also determines which array goes where in the sharedObject data.

parentMC.sharedObject.data.moveSpdUpgrade = parentMC.upgrades.tempMoveSpdUpgrade;
parentMC.sharedObject.flush();

However, when I modify the tempMoveSpdUpgrade array, it also saves the new data in the sharedObject even if the flush hasn't been called yet.

tempMoveSpdUpgrade[0][2] = 1;
trace(parentMC.sharedObject.data.moveSpdUpgrade);

This trace shows that the data has changed but I don't understand since the flush hasn't been called and the swf hasn't been closed. I'm wondering why the modifications made in the array automatically changes the sharedObject data.

Thank you for the help.

Edit:

public function saveGame(){  
    parentMC.sharedObject.data.money = parentMC.money;  
    parentMC.sharedObject.data.moveSpdUpgrade = parentMC.upgrades.tempMoveSpdUpgrade;  
    parentMC.sharedObject.flush();  
}

Like I stated in the comments with hackattack, the money is the correct data when I don't save but the moveSpdUpgrade array is modified either way.

oliboon
  • 351
  • 1
  • 5
  • 19

2 Answers2

0

What is in the sharedObject that is loaded into memory and what is saved to disc are separate things. You are tracing the sharedObject in memory. Until you flush this sharedObject it probably wont save its image to disc. Try creating a new SharedObject and tracing that, it should be different.

Edit:

I see an error in what i told you to do before (below in the comments). I told you to make a new instance of moveSpdUpgrade to place in your shared object with the .concat method. The reason this doesnt work is because moveSpdUpgrade is a 2d array, i missed this detail. So calling .concat creates a new instance, but the new instance is still filled with references to arrays and it those arrays that you are editing. So to solve this we have to do a deep copy of moveSpdUpgrade. Here is a function that will accomplish that

function array2DConcat(target : Array) : Array {
    var buff : Array = new Array(target.length);
    for(var i : int = 0;i < target.length;i++) }
        buff[i] = target[i].concat();
    }
    return buff;
}

and then if you change your method to

public function saveGame(){  
    parentMC.sharedObject.data.money = parentMC.money;  
    parentMC.sharedObject.data.moveSpdUpgrade = array2DConcat(parentMC.upgrades.tempMoveSpdUpgrade);  
    parentMC.sharedObject.flush();  
}

hopefully that will work.

hackattack
  • 1,087
  • 6
  • 9
  • Currently, even if I don't use the save button that calls flush(), the data in this array is loaded with all recent modifications instead of the last version that was saved. – oliboon Jul 23 '12 at 23:40
  • it looks like if you don't call flush directly, "Flash Player writes the shared object to a file when the shared object session ends". I would make a new instance of the array tempMoveSpdUpgrade to put in your shared object so you can modify tempMoveSpdUpgrade without affecting the shared object. like so: parentMC.sharedObject.data.moveSpdUpgrade = parentMC.upgrades.tempMoveSpdUpgrade.conact() – hackattack Jul 23 '12 at 23:45
  • What is weird is that I'm trying without even closing the swf. I'm simply returning to the main menu and loading the savedgame. I tried your method and it still seems to be modifying the sharedObject when the array is modified. – oliboon Jul 24 '12 at 00:02
  • that is weird. If you post more code i can try to help you further, but with the information given that is the best i can do. good luck. – hackattack Jul 24 '12 at 00:10
  • I've found something else that is strange, I also save the player money when I call my save game function. This variable will show the right amount even if it is saved using the same method and in the same function as the moveSpdUpgrade array. For some reason, the money variable isn't saved when the saveGame function isnt called but the array is. – oliboon Jul 24 '12 at 00:19
  • I just edited my function in. As you can see it's really simple, but the money is working(is saved only when I call savegame function) and the array isn't. – oliboon Jul 24 '12 at 00:28
  • I've made the modification in my code but I'm now getting an error for moveSpdUpgrade[0][2] stating that it is undefined. Is there any modification that needs to be made in the load game? `tempMoveSpdUpgrade = parentMC.sharedObject.data.moveSpdUpgrade;` – oliboon Jul 24 '12 at 00:54
  • I'm sorry, I'm not sure. Hopefully I got you on the right track, but it seems there is more going on here then I can help you with. – hackattack Jul 24 '12 at 00:58
  • Thank you for the help. However, when I try to trace the parentMC.sharedObject.data.moveSpdUpgrade, I only get ",," . It seems it has been concatenation worked but it seems I've lost the data. I have to leave right now but I will try to fix this tomorrow. – oliboon Jul 24 '12 at 01:04
0

I think you are running into a bit confusing thing about arrays. What is happening never saves any data by another function or has something to do with the rest of your code.

Simple the method how you try to copy the array of that Sharedobject doesnt create two seperate Obejcts. Instead you are always working live on the sharedObject-array.

package
{
  import flash.display.Sprite;

  public class arraytester extends Sprite
  {

    private var a:Array = new Array(1,2,3,4,5);
    private var b:Array = new Array("a","b","c","d","e","f");

    public function arraytester()
    {

    // Both arrays are different Objects
        trace(a); // 1,2,3,4,5
        trace (b); //a,b,c,d,e,f

    // Both arrays are pointing to the same Object
        a= b;
        trace(a); //a,b,c,d,e,f
        trace (b); //a,b,c,d,e,f

        b[1] = 2;
        trace(a); //a,2,c,d,e,f
        trace (b); //a,2,c,d,e,f

    // a is a copy of b while becoming a seperate Object
        a = b.concat();
        b[0]= 1;
        trace(a); //a,2,c,d,e,f
        trace (b); //1,2,c,d,e,f
    }
  }
}
Sidrich2009
  • 570
  • 1
  • 4
  • 11
  • I tried to create a new array moveSpdUpgrade and use this one for the save instead. `moveSpdUpgrade = tempMoveSpdUpgrade.concat(); tempMoveSpdUpgrade[0][0] = 0; trace(moveSpdUpgrade); trace(tempMoveSpdUpgrade);` However, the two traces shows the same thing even if it's been changed after the concat. – oliboon Jul 24 '12 at 20:18
  • I realized that your example was with a 1dimension array. I've modified it to fit with my code and it works now.. Thank you so much.. I needed that refresh about the arrays apparently – oliboon Jul 24 '12 at 21:00