- Home /
I cant get my Xml save script to work. I dont know how.
I use Javascript
As the title states, i cant figure out xml and the tutorials i find dont explain it very well either. I looked at the wiki and tried to make this script below, but i have no idea how to write it out.. my "stats" are in a seperate script names Statistics.js im attempting to call those variables and save them. What should i do to fix this script?
import System.Xml;
import System.Xml.Serialization;
public var myVit : int;
public var myName : String;
function Update(){
myVit = Statistics.playerVit;
myName = Statistics.playerName;
}
public class SaveData
{
myVit;
myName;
}
// save
function Save(){
var serializer : XmlSerializer = new XmlSerializer(typeof(SaveData));
var stream : Stream = new FileStream(path, FileMode.Create);
serializer.Serialize(stream, this);
stream.Close();
}
//load
function Load(){
var serializer : XmlSerializer = new XmlSerializer(typeof(SaveData));
var stream : Stream = new FileStream(path, FileMode.Open);
var saveFile : SaveData = serializer.Deserialize(stream) as SaveData;
stream.Close();
}
//------------------EDIT-------------------//
//-------UPDATED SCRIPT---------//
import System.Xml;
import System.Xml.Serialization;
import System.IO;
public class SaveData
{
public var myVit : int;
public var myName : String;
function Save(){
var serializer : XmlSerializer = new XmlSerializer(typeof(SaveData));
var stream : Stream = new FileStream(path, FileMode.Create);
serializer.Serialize(stream, this);
stream.Close();
}
function Load(){
var serializer : XmlSerializer = new XmlSerializer(typeof(SaveData));
var stream : Stream = new FileStream(path, FileMode.Open);
var saveFile : SaveData = serializer.Deserialize(stream) as SaveData;
stream.Close();
}
function Write(){
SaveData.Save(Path.Combine(Application.persistentDataPath, "Data.xml"));
}
function Read(){
var myData : SaveData = SaveData.Load(Path.Combine(Application.dataPath, "Data.xml"));
}
}
i attempted to fix the script, this is what i came up with but i still get errors.
13,46 Unknown identifier: 'path'.
20,46 Unknown identifier: 'path;.
26,18 An instance of type 'SaveData' is required to access non static member 'Save'.
30,42 An instance of type 'SaveData' is required to access non static member 'Load'.
//------------------EDIT #2----------------------------//
import System.Xml;
import System.Xml.Serialization;
import System.IO;
public class SaveData
{
public var path : String = "";
public var myVit : int;
public var myName : String;
function Save(){
var serializer : XmlSerializer = new XmlSerializer(typeof(SaveData));
var stream : Stream = new FileStream(path, FileMode.Create);
serializer.Serialize(stream, this);
stream.Close();
}
function Load(){
var serializer : XmlSerializer = new XmlSerializer(typeof(SaveData));
var stream : Stream = new FileStream(path, FileMode.Open);
var saveFile : SaveData = serializer.Deserialize(stream) as SaveData;
stream.Close();
}
function Write(){
SaveData.Save(Path.Combine(Application.persistentDataPath, "Data.xml"));
}
function Read(){
var myData : SaveData = SaveData.Load(Path.Combine(Application.dataPath, "Data.xml"));
}
function SaveText(){
var sd : SaveData = new SaveData();
sd.myVit = 10;//or something
sd.myName = "Name";
sd.Save("PATH TO FILE");
}
}
this is my most recent code.
Errors:
26,18 An instance of type 'SaveData' is required to access non static member 'Save'.
30,42 An instance of type 'SaveData' is required to access non static member 'Load'.
36,16 The best overload for the method 'SaveData.Save()' is not compatible with the argument list '(String)'.
/--------------Edit Newest Script----------------/
import System.Xml;
import System.Xml.Serialization;
import System.IO;
public class SaveData extends MonoBehaviour
{
public var path : String = "Application.dataPath";
public var myVit : int;
public var myName : String;
function Save(path : String){
var serializer : XmlSerializer = new XmlSerializer(typeof(SaveData));
var stream : Stream = new FileStream(path, FileMode.Create);
serializer.Serialize(stream, this);
stream.Close();
}
static function Load(path : String){
var serializer : XmlSerializer = new XmlSerializer(typeof(SaveData));
var stream : Stream = new FileStream(path, FileMode.Open);
var saveFile : SaveData = serializer.Deserialize(stream) as SaveData;
stream.Close();
return saveFile;
}
function Write(){
Save(Path.Combine(path, "Data.xml"));
}
function Read(){
var myData : SaveData = SaveData.Load(Path.Combine(path, "Data.xml"));
}
function SaveText(){
var sd : SaveData = new SaveData();
sd.myVit = 10;//or something
sd.myName = "Name";
sd.Save(path);
}
}
I just formatted your code. You can do this by highlighting all your code, then clicking the 10101 button at the top of the edit window =]
Edit : no worries, wanted to do it so you could get an answer.
i did format it... it always messes up on me, i usually edit 4 or 5 times just to get it right, sorry about that..
Answer by $$anonymous$$ · Jan 30, 2013 at 09:00 AM
I use C# and when you use the XmlSerializer you have to make sure all your variables are public. So try to do that and see of it works. And since you Serialize SaveData make sure you store your data in there ;)
Do you get an error message when you run the code?
EDIT:
public class SaveData
{
public var myVit : int;
public var myName : string;
function Save(path : string){
var serializer : XmlSerializer = new XmlSerializer(typeof(SaveData));
var stream : Stream = new FileStream(path, FileMode.Create);
serializer.Serialize(stream, this);
stream.Close();
}
}
Somewhere else in your code:
var sd : SaveData = new SaveData();
sd.myVit = 10;//or something
sd.myName = "Name";
sd.Save("PATH TO FILE");
EDIT for new code
You are doing a few things wrong:
function Write(){
SaveData.Save(Path.Combine(Application.persistentDataPath, "Data.xml"));
}
SaveData is your class and you cannot call non static functions this way. Since this function is in the same class just use
function Write(){
Save(Path.Combine(Application.persistentDataPath, "Data.xml"));
}
then: function Save(){ change it to function Save(path : string){
your load function is not correct, your load function should be made static and has to have a return type
static function Load() : SaveData{
var serializer : XmlSerializer = new XmlSerializer(typeof(SaveData));
var stream : Stream = new FileStream(path, FileMode.Open);
var saveFile : SaveData = serializer.Deserialize(stream) as SaveData;
stream.Close();
return saveFile;
}
That should work, if you have errors make sure the you check the format for JavaScript since I don't use it, I don't know if it is correctly written.
i have Static variables that im saving. or public only, from what i can tell.
Edit: And also yes i get an error, it says my variables are not right, between line 15-20. they are set to public at line 5 and updated at line 10.
Have updated my answer, and I noticed that your Save function is not in the SaveData class. So then you cannot use serializer.Serialize(stream, this); because this is not of the type SaveData. You can put the function in SaveData or use an object of SaveData ins$$anonymous$$d of this
Ok, that makes sense, but would the variables update without being inside of an update fuction if they are listed inside the class only? i was attempting to update my variables in game, and then save the current values upon clicking my save button by calling Save() from my GUI script. So i wrote this:
myVit = Statistics.playerVit;
to find the variable i wanted to save(playerVit) from my script named Statistics.
If nothing else i could just write my statistics here ins$$anonymous$$d... and call them from other scripts ins$$anonymous$$d of having this script call them... Any suggestions?
You can update your variables in the class before you are going to save it. Or maybe pass them as arguments to the save function. Or else update them when they change.
ok, thanks, i tried the new script above, but still doesnt work. it gave the errors added also, in the Edit i made to the question.
Answer by Imankit · Jan 30, 2013 at 10:52 AM
first you have to assign data type to your all variable, xml serialize will crash without it....
and be sure you are using .Net 2.0 subset and striping is disabled in player setting
i did this, though i didnt see options for stiping my project is using .Net 2.0 subset. and my varialbes all have a type.
It is in Player Settings..
Go to Edit/ProjectSettings/Player
You will see it in Inspector... In that there is Api Compatibility Level... $$anonymous$$ake it to .Net 2.0 Subset And make theStripping Level Disabled
Your answer
Follow this Question
Related Questions
Save data through xml Serialization on iOS 1 Answer
Saving Game Problem 1 Answer
Retrieving basic Strings and Ints from XML Files (JS) 0 Answers