Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Punkjim420 · Jan 30, 2013 at 07:27 AM · javascriptserializationsaveloadingxml

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);
     }
  
 }
Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image AlucardJay · Jan 30, 2013 at 08:30 AM 0
Share

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.

avatar image Punkjim420 · Jan 30, 2013 at 11:23 PM 0
Share

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..

avatar image Punkjim420 · Jan 31, 2013 at 01:59 AM 0
Share

well then, thank you alucardj

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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.

Comment
Add comment · Show 18 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Punkjim420 · Jan 30, 2013 at 11:26 PM 0
Share

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.

avatar image $$anonymous$$ · Jan 30, 2013 at 11:39 PM 0
Share

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

avatar image Punkjim420 · Jan 31, 2013 at 12:00 AM 0
Share

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?

avatar image $$anonymous$$ · Jan 31, 2013 at 07:01 AM 0
Share

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.

avatar image Punkjim420 · Jan 31, 2013 at 07:30 AM 0
Share

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.

Show more comments
avatar image
1

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

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Punkjim420 · Jan 30, 2013 at 11:39 PM 0
Share

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.

avatar image Imankit · Feb 05, 2013 at 06:18 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Save data through xml Serialization on iOS 1 Answer

serialization (save and load game) on a javascript based project, should i/can i use c#, or should stick with javascript? 1 Answer

Saving Game Problem 1 Answer

Retrieving basic Strings and Ints from XML Files (JS) 0 Answers

Serialized data not getting saved in iPhone 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges