Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Deserialize Syntax?

Discussion in 'Scripting' started by Cooper37, Jan 15, 2015.

  1. Cooper37

    Cooper37

    Joined:
    Jul 21, 2012
    Posts:
    383
    Hi, Unity Community! I'm trying to experiment using persistantDataPath for Java/Unity Script, but I've became stuck. How can I correct line 6 which is the C# version of this but I'm shooting for Java, and could someone also tell me if line 10 is correct in Java? I don't know what the Java version of C#'s [Serializable]. Thank you! :)
    Code (csharp):
    1.  
    2. public function Load(){
    3.     if(File.Exists(Application.persistentDataPath + "/SavedGame.ink")){
    4.         bf = new BinaryFormatter();
    5.         file = File.Open(Application.persistentDataPath + "/SavedGame.ink");
    6.         data = (SavedData)bf.Deserialize(file);
    7.         file.Close();
    8.     }
    9. }
    10. @SerializeField
    11. class SavedData{
    12.     var savePosition : Vector3;
    13. }
    14.  
     
  2. inko

    inko

    Joined:
    Nov 29, 2013
    Posts:
    143
    Hey man!

    Your serialization code looks fine to me. Unfortunately you can't serialize Vector3 structs. (which is a huuuuge pain I might add!).
    To fix this you either write a wrapper class and serialize it yourself or you just do something like:

    class savedData {
    float savePosX;
    float savePosY;
    float savePosZ;
    }

    Oh, and little side note. You mean java script. Not java. They have actually very little in common. Even c# is closer to Java then java script is. Yah, I know, pretty weird eh?;)
     
    TheForsaken95 likes this.
  3. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,184
    JavaScript and Java are not the same thing. Not by a long shot. You're looking for Javascript help.

    JavaScript is named JavaScript because Java was really fresh and cool when JavaScript came out. They barely look and they function completely differently. There is no Java in Unity - the closest thing is actually C#, not JavaScript

    #rant
     
  4. cmcpasserby

    cmcpasserby

    Joined:
    Jul 18, 2014
    Posts:
    315
    Also to add to the language rant, what unity calls JavaScript iaht the same JavaScript used in web development.
     
  5. Cooper37

    Cooper37

    Joined:
    Jul 21, 2012
    Posts:
    383
    Good idea! I had to do the same thing when I used PlayerPrefs but I didn't know it wouldn't work with serialized data. Also, thanks for clearing up the Java/Java Script thing(ALL OF YOU! LOL)

    However, I still get an error saying that I'm missing a semicolon on line 6, so I really believe there's a syntax error. Remember, line 6 is C#, and I need help converting it to Java Script.
     
  6. inko

    inko

    Joined:
    Nov 29, 2013
    Posts:
    143
    Had to actually look that up myself but aparently unity javascript does type casting a little different (found this: LINK ).

    What your line 6 is currently doing is calling a function called Deserialize with the parameter "file". Deserialize returns data of type Object. Now usually you need to tell your programm to convert your data to whatever type you need it as. In your case that would be SavedData.

    But apparently Unity JavaScript does that on its own. So if you change line 6 to:

    Code (JavaScript):
    1. data = bf.Deserialize(file);
    or

    Code (JavaScript):
    1. data = bf.Deserialize(file) as SavedData;
    and it should work.
     
  7. Cooper37

    Cooper37

    Joined:
    Jul 21, 2012
    Posts:
    383
    Thank you, you're awesome. :) It worked! Here's the full script if anyone is interested. If anyone has any idea how I can save other things like object information, SetActive information, game state/progression, etc, please lend some advice. Thanx:
    js.
    Code (csharp):
    1.  
    2. import System;
    3. import System.IO;
    4. import System.Runtime.Serialization.Formatters.Binary;
    5. var player : Transform;
    6. function Start(){
    7.     player = GameObject.FindGameObjectWithTag("Player").transform;
    8. }
    9. function Update(){
    10.     if(Input.GetKeyDown("s"))
    11.         Save();
    12.     if(Input.GetKeyDown("l"))
    13.         Load();
    14. }
    15. public function Save(){
    16.     bf = new BinaryFormatter();
    17.     file = File.Create(Application.persistentDataPath + "/SavedGame.ink");
    18.     data = new SavedData();
    19.  
    20.     data.X = transform.position.x;
    21.     data.Y = transform.position.y;
    22.     data.Z = transform.position.z;
    23.  
    24.     bf.Serialize(file,data);
    25.     file.Close();
    26. }
    27. public function Load(){
    28.     if(File.Exists(Application.persistentDataPath + "/SavedGame.ink")){
    29.         bf = new BinaryFormatter();
    30.         file = File.Open(Application.persistentDataPath + "/SavedGame.ink",FileMode.Open);
    31.         data = bf.Deserialize(file) as SavedData;
    32.         file.Close();
    33.    
    34.         player.transform.position.x = data.X;
    35.         player.transform.position.y = data.Y;
    36.         player.transform.position.z = data.Z;
    37.     }
    38. }
    39. @SerializeField
    40. class SavedData{
    41.     var X : float;
    42.     var Y : float;
    43.     var Z : float;
    44. }
    45.  
     
    TheForsaken95 likes this.