I've been trying to figure out how to write a proper save/load script using java, but haven't had much luck. It seems PlayerPrefs is a pretty common (and fairly easy) way of going about it, but apparently it isn't that secure. I followed the tutorial [here][1], but I am using javascript in my project. I know it's possible to translate it into javascript as I have read several posts from users saying they have done it, but I haven't been able to find any written script that shows what needs to be changed. I have translated it as far as I can and only have one error appearing now. I'll post my script plus the error below.
var saveVariable : int;
public static var control : SaveScript;
function Awake()
{
if(control == null)
{
DontDestroyOnLoad(transform.gameObject);
control = this;
}
else if(control != this)
{
Destroy(gameObject);
}
}
public function Save()
{
var bf : BinaryFormatter = new BinaryFormatter();
var file : FileStream = FileCreate(Application.persistentDataPath + "/playerInfo.dat");
var data : PlayerData = new PlayerData();
data.saveVariable = saveVariable;
bf.Serialize(file, data);
file.Close();
}
public static function Load()
{
if(File.Exists(Application.persistentDataPath + "/playerInfo.dat"))
{
var bf : BinaryFormatter = new BinaryFormatter();
var file : FileStream = File.Open(Application.persistentDataPath + "/playerInfo.dat", FileMode.Open);
////////////////THIS IS THE LINE THE ERROR APPEARS ON/////////////
var data : PlayerData = (PlayerData)bf.Deserialize(file);
//////////////////////////////////////////////////////////////////
file.Close();
saveVariable = data.saveVariable;
}
}
[Serializable];
public class PlayerData
{
public var saveVariable : int;
}
The error that appears is:
Assets/Resources/Scripts/SaveScript.js(43,53): UCE0001: ';' expected. Insert a semicolon at the end.
I know this alone might not fix it, because this may be one of those cases where fixing one error can cause 10 more to appear, but I guess I'm mainly just hoping someone can tell me what I have wrong or point me in the right direction. Thanks for any help!
[1]: https://unity3d.com/learn/tutorials/topics/scripting/persistence-saving-and-loading-data
↧