I was using the Javascript serializer in my latest game for save data, but when I try to run it after compiling it to Windows stand-alone, it loads all the files as a blank entity and it does not save them.
Everything else appears to work fine-- everything that’s in Unity-- so I think the problem is that it’s not exporting.
The whole script file:
import System.Xml;
import System.Xml.Serialization;
import System.Collections.Generic;
import System.IO;
…
public class GameVars
{
private static var instance : GameVars = new GameVars();
public var switches : boolean;
public var mouseSensitivity : double = 5.0;
public var data : GameData = new GameData();
public var fileID : int = 1;
public static function getInstance()
{
if(instance == null)
instance = new GameVars();
return instance;
}
public function RunNextLevel()
{
data.levelID++;
if(data.levelID == 0)
data.levelID = 1; //You were in test level, move on to next level!
data.checkPointID = 0;
for(var s : boolean in switches)
s = false;
data.Save();
Application.LoadLevel("Loading");
}
public function GameVars()
{
switches = new boolean[5];
for(var s : boolean in switches)
{
s = false;
}
}
public function SaveCheckpointData()
{
var player = GameObject.Find("Player");
data.checkPointID++;
data.playerX = player.transform.position.x;
data.playerY = player.transform.position.y;
data.playerZ = player.transform.position.z;
data.lookY = player.transform.rotation.eulerAngles.y;
data.playerHealth = player.GetComponent(HealthArmor).health;
data.playerArmor = player.GetComponent(HealthArmor).armor;
var wepList : WeaponList = WeaponList.getInstance();
data.weaponID = wepList.weaponID;
var i : int;
for(i = 0; i < 8; i++)
data.isHolding _= wepList.weapons*.isPickedUp;*_
* for(i = 0; i < 6; i++)*
data.bulAmmo = wepList.bullets*.ammo;*
* //data.Save(Path.Combine(Application.persistentDataPath, “save1.xml”));*
* data.Save();*
* }*
}
@XmlRoot(“GameData”)
public class GameData
{
* //Data to save*
* public var fileName : String;*
* public var levelID : int = 1; //Starton the test level*
* public var checkPointID : int = 0;*
* public var playerX : double = 0.0;*
* public var playerY : double = 0.0;*
* public var playerZ : double = 0.0;*
* public var lookY : double = 0.0;*
* public var playerHealth : double = 100;*
* public var playerArmor : double = 0;*
* public var weaponID : int = 0;*
* public var immortal : boolean = false;*
* public var ammoInfinite : boolean = false;*
* @XmlArray(“Weapons”)*
* @XmlArrayItem(“IsHolding”)*
* public var isHolding : boolean[];*
* @XmlArray(“Bullets”)*
* @XmlArrayItem(“Amount”)*
* public var bulAmmo : int[];*
* public function ResetToDefault()*
* {*
* levelID = 1;*
* checkPointID = 0;*
* playerX = 0;*
* playerY = 0;*
* playerZ = 0;*
* lookY = 0;*
* playerHealth = 100;*
* playerArmor = 0;*
* weaponID = 0;*
* immortal = false;*
* ammoInfinite = false;*
* var i : int;*
* for(i = 0; i < 8; i++)*
_ isHolding = false;_
* for(i = 0; i < 6; i++)*
_ bulAmmo = 0;_
* }*
* public function GameData()*
* {*
* isHolding = new boolean[8];*
* bulAmmo = new int[6];*
* ResetToDefault();*
* }*
* public function Save()*
* {*
* if(levelID == -1)*
* return; //Don’t save the file on test level*
* try*
* {*
* var path : String = “saves/save” + GameVars.getInstance().fileID + “.xml”;*
* var serializer : XmlSerializer = new XmlSerializer(GameData);*
* var stream : Stream = new FileStream(path, FileMode.Create);*
* serializer.Serialize(stream, this);*
* stream.Close();*
* Debug.Log("Game saved! ");*
* }*
* catch(e2 : DirectoryNotFoundException)*
* {*
* System.IO.Directory.CreateDirectory(“saves/”);*
* Save();*
* }*
* }*
* public static function Load() : GameData*
* {*
* try*
* {*
* var path : String = “saves/save” + GameVars.getInstance().fileID + “.xml”;*
* var serializer : XmlSerializer = new XmlSerializer(GameData);*
* var stream : Stream = new FileStream(path, FileMode.Open);*
* var result : GameData = serializer.Deserialize(stream) as GameData;*
* stream.Close();*
* return result;*
* }*
* catch(e : FileNotFoundException)*
* {*
* Debug.Log(“File not found!”);*
* return null;*
* }*
* catch(e2 : DirectoryNotFoundException)*
* {*
* Debug.Log(“Directory has not been created yet”);*
* return null;*
* }*
* }*
}