XMLserialization in javascript problems

i found alot of c# information about XMLserialization but not much for javascript.
im working on a small 2d game where i want the score of the player is saved (automatic). so when the player closes the game the score wont be lost. when you play the game again it starts again from the point where you left. i dont want to do this with PlayerPrefs. i want to do this with XMLserialization but i dont know how. can some one help me? explain me how to do this in javascript?

thanks alot,
kelvin

Well a number of things:

  1. I wouldn’t serialize things as score to XML. Personally I would save it at least as binary data as that is harder to manipulate (would be enough for ~99% of the people potentially playing your game). This is especially important when you begin making games with online components like scoreboards so it’s a pretty good practice to also do when making a simple game. How to save binary data? Well I have a sample in my signature, though it is in C#.

  2. If possible switch to C#. There are, as you already noticed, way more resources online for C#, new tutorials from Unity themselves are in C#, and 90% of the forums posters around here use C#. Also the fact that UnityScript is NOT JavaScript but a JavaScript like implementation of a scripting language also makes learning the syntax less useful outside of Unity, while C# can be used for more then just Unity applications. Also, in my signature there is a link to an excellent learning resource for C#, so be sure to check that out.

is there a way to make the saveGame option in C# and activate it with javascript. and collect the score from javascript into c#?
also this is my first game. its more just to learn for me than it is going to be a real game.

Please don’t harass people about language choices. If you don’t want to explain how to use it in Unityscript then ignore the topic. In any case, the usage isn’t functionally any different in Unityscript compared to C#, you just use Unityscript syntax. Going by the first several lines of a C# example on MSDN:

private void SerializeDataSet(string filename){
    XmlSerializer ser = new XmlSerializer(typeof(DataSet));

    // Creates a DataSet; adds a table, column, and ten rows.
    DataSet ds = new DataSet("myDataSet");
    DataTable t = new DataTable("table1");
    DataColumn c = new DataColumn("thing");
    t.Columns.Add(c);

The Unityscript version would be (make sure to import System.Xml.Serialization and System.Data):

private function SerializeDataSet (filename : String) {
    var ser = new XmlSerializer(DataSet);

    // Creates a DataSet; adds a table, column, and ten rows.
    var ds = new DataSet("myDataSet");
    var t = new DataTable("table1");
    var c = new DataColumn("thing");
    t.Columns.Add(c);

Note that the Unityscript code inside the function would work just as well if it were copied to the C# example, aside from having to use typeof in new XmlSerializer, since C# does implicit typing exactly the same way as Unityscript. If you want to use explicit typing, then declare it like “var t : DataTable = new…”. The compiled code is the same either way; it’s not dynamic typing, just a compiler convenience. (My preference is to use implicit typing in both C# and Unityscript if the type is already obviously specified by what you’re declaring, since with explicit typing I find the redundancy a bit confusing visually, especially for long types.)

–Eric

im so sorry that i ask you this. but i really dont understand it (i started learning javascript 2 weeks ago).
where can i save my scores? where is the file saved? how can i load it?
can you please explain it to me?

thanks alot,
kelvin

That’s a different question, but saving and loading files are done with System.IO classes. Look at the bottom of the example on the MSDN page I linked to, with the StreamWriter that uses the filename passed into the function. However this might be too advanced if you’re just starting, so perhaps it would be best to do simpler things for a while until you get comfortable with coding.

–Eric