Monday 8 July 2013

As3 Saving & Loading with SharedObjects

Creating a Save Function AS3? 

So basically I've never actually implemented an actual saving & loading function in a game before, but I figured it was about time I pulled my finger out and get experimental. 

I have a brief experience working with SharedObject (the class used to create cookies), and I remember that I had to individually specify the properties to be saved or loaded :

demoVariable = saveFile.data.demoVariableTwo

This being incredibly tedious as you would have to write a line of code for each and every variable used, not to mention you would have to write the reverse for both loading and saving. Which immediately got me thinking, there must be a faster way (being the diligent, enthusiastic,  hard worker that I am). 

So I started experimenting with arrays & planned to hopefully use a loop function to interchange the values of each. as an example I had the saveArray:Array which would be the in game properties such as expPoints, currentLevel etc & the loadArray:Array which would be the properties that were saved onto the computer. By having the two arrays arranged with corresponding variables (from each array) in the same index order, by using a loop function, I could accurately transfer the values from each variable.
But I soon realised & remembered that arrays use references and not values, meaning that if I used;

for (var i:Number = 0; i < saveArray.length; i ++)
{                                                                           
    saveArray[i] = loadArray[i];                                 
}                                                                                

The property stored in saveArray[i] slot wouldn't become the same as the value in loadArray[i], but the slot itself would become the value in the loadArray[i] slot. 

So going back to the drawing board I was trying to think of a way that I could easily transfer the two properties, and I in fact found a method that was faster and more efficient. I created an array loadArray:Array and I created string variables for each slot which had the same name as  the variables of the game counters which were in the my class PlayerMain (eg "currentLevel", "Exp"). By using the string in the array as a reference for the variable I was searching for in PlayerMain I could create a new variable in SharedObjects which would be conveniently named the same as the variable it remembers  and also give it the same property. 


Exp3 class : 

public function GetLoad(getNum:Number):String    
{                                                         
return loadArray[getNum];                                 

}                                                                                 


Save class : 

for (var i:Number = 0; i < Exp3.loadArray.length; i ++)
{                                                                                   
var thisOne:String = Exp3.instance.GetLoad(i);        
saveData.data[thisOne] = PlayerMain[thisOne];         
saveData.flush();                            

}                                                                                      


Another highly convenient thing about this is the fact that to load saved data you only need to reverse the order in which the saveData and PlayerMain are in, also by using the same array for both loading and saving variables, if I accidentally misspelled  a variable which was saved and therefore made a saved variable null, when loading it wouldn't replace the current variable as the variable name of the saved property doesn't match, therefore not potentially breaking the game by nullifying a necessary variable. 



No comments:

Post a Comment