Greetings noble forum goers. I’ve been beating my head against this wall for well over a week now and it feels like it’s time to ask for help. I’ve been attempting to implement persistence within a game I’m making (specifically, level scores) and no matter what I do, I can’t seem to make it remember my score after I close the swf and recompile. The really frustrating part is that I’ve successfully designed a SaveLoad class (code below) and created a little test applet which works perfectly (I’ve attached the .fla and .as file I used to make that applet).
In a nutshell, the problem is that when I try to utilize the Persist class (code also included below) for my game, it doesn’t recognize any previous data. Even though I’m still in testing so the constructor never gets called with anything but a hard “testing” String and the fileName setter is never invoked, no data exists in the SharedObject.getLocal().data created within the SaveLoad class. Through strategic use of trace statements and conditionals, I’ve learned that the saveInt function gets called successfully, but whenever the loadInt function is called (even the second time through after a successful save) it finds no data associated with the file and parameter given, despite the fact that those names are always exactly the same at this stage in testing.
I’m frustrated and I’m really hopeful that I’ll be able to find some insight from these forums - even if it’s just a more specific location within my code where I may need to look.
import flash.net.SharedObject;
public class SaveLoad
{
private var fileName:String;
private var saveData:Object;
public function SaveLoad(newFileName:String):void
{
fileName = stripString(newFileName);
saveData = SharedObject.getLocal(fileName).data;
}
public function paramExists(paramName:String):Boolean
{
paramName = stripString(paramName);
return (saveData[paramName] != null);
}
public function isInt(paramName:String):Boolean
{
paramName = stripString(paramName);
return saveData[paramName] is int;
}
public function saveInt(paramName:String, toSave:int):void
{
paramName = stripString(paramName);
if (saveData[paramName] == null || saveData[paramName] is int)
saveData[paramName] = toSave;
}
public function loadInt(paramName:String):int
{
paramName = stripString(paramName);
return saveData[paramName] as int;
}
//<snip is, save and load functions for Numbers, Strings and Booleans>
public function clearAllData():void
{
var paramName:String;
for (paramName in saveData)
delete saveData[paramName];
}
private function stripString(oldString:String):String
{
var toReturn:String = "";
var currentChar:String;
for (var index:int = 0; index < oldString.length; index++)
{
currentChar = oldString.charAt(index);
if (currentChar != "~" && currentChar != "%" && currentChar != "&" &&
currentChar != ";" && currentChar != ":" && currentChar != "\"" &&
currentChar != "'" && currentChar != "," && currentChar != "<" &&
currentChar != ">" && currentChar != "?" && currentChar != "#" &&
currentChar != " ")
{
toReturn += currentChar;
}
}
return toReturn;
}
}
import Basics.Persistence.*;
public class Persist
{
private var saveData:SaveLoad;
public function Persist(newFileName:String):void
{
fileName = newFileName;
}
public function set fileName(newFileName:String):void
{
if (saveData != null)
saveData.nullify();
saveData = new SaveLoad("GameName||" + newFileName);
}
public function getLevelScore(world:int, level:int):int
{
var paramName:String = levelScoreParam(world,level);
if (saveData.paramExists(paramName))
return saveData.loadInt(paramName);
else
return -1;
}
public function setLevelScore(world:int, level:int, newScore:int):void
{
var paramName:String = levelScoreParam(world,level);
saveData.saveInt(paramName, newScore);
}
public function clearAllData():void
{
saveData.clearAllData();
}
private function levelScoreParam(world:int, level:int):String
{
return "levelScore|" + world + "-" + level;
}
}