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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

problems writing a list to binary file

Discussion in 'Scripting' started by jackson_31, May 2, 2015.

  1. jackson_31

    jackson_31

    Joined:
    Sep 20, 2014
    Posts:
    84
    I am trying to create a simple level editor where I can store the users selections as a list of integers and strings

    here is my class code

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. public class SomeClass : MonoBehaviour
    4. {
    5.     public string name;
    6.     public int power;
    7.    
    8.     public SomeClass(string newName, int newPower)
    9.     {
    10.         name = newName;
    11.         power = newPower;
    12.     }
    13.    
    14. }

    then in another Script I have this
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using System.IO;
    5. using System.Runtime.Serialization.Formatters.Binary;
    6. using System.Linq;
    7. using System.Text;
    8. using System.Runtime.Serialization;
    9.  
    10. public class Automate1 : MonoBehaviour
    11. {
    12.  
    13.  
    14.     public static void WriteToBinaryFile<T>(string filePath, T objectToWrite, bool append = false)
    15.     {
    16.         using (Stream stream = File.Open(filePath, append ? FileMode.Append : FileMode.Create))
    17.         {
    18.             var binaryFormatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
    19.             binaryFormatter.Serialize(stream, objectToWrite);
    20.         }
    21.     }
    22.  
    23.     public static T ReadFromBinaryFile<T>(string filePath)
    24.     {
    25.         using (Stream stream = File.Open(filePath, FileMode.Open))
    26.         {
    27.             var binaryFormatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
    28.             return (T)binaryFormatter.Deserialize(stream);
    29.         }
    30.     }
    31.  
    32.     void Start ()
    33.     {
    34.  
    35.         List<SomeClass>  ff = new List<SomeClass>();
    36.         ff.Add( new SomeClass("Harvey", 50));
    37.         ff.Add( new SomeClass("Magneto", 100));
    38.         ff.Add( new SomeClass("Pip", 5));
    39.  
    40.         WriteToBinaryFile<SomeClass>("C:\\someClass.txt",ff);
    41.         SomeClass  test = MyBinary.ReadFromBinaryFile<SomeClass>("C:\\someClass.txt");
    42.         Debug.Log (test);
    43.  
    44.  
    45. }

    but then i get an error: " No overload for method `WriteToBinaryFile' takes `2' arguments"
     
  2. ericmartinez

    ericmartinez

    Joined:
    Feb 26, 2014
    Posts:
    9
    That seems weird. I've been doing some research and optional parameters are available since C# 4.0. I'm not an expert C# programmer, I'm learning it along with Unity.

    I found this : http://stackoverflow.com/questions/199761/how-can-you-use-optional-parameters-in-c

    Unity uses MonoDevelop for writing C# code, so it's not a Unity problem but Mono's. Do you know what version do you have? I have MonoDevelop 4.0.1 and Unity 5.0.1 and I tried this little piece of code :

    Code (csharp):
    1.  
    2.     void Test(string first, string second = "default value") {
    3.         Debug.Log (first+ " - " + second);
    4.     }
    5.  
    And it works correctly.
     
  3. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Unity's version of Mono can get weird with optional parameters on generic methods. It's typically easier to overload the method instead. Have the two parameter method call the three parameter method with the default optional parameter.
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    No, Mono and MonoDevelop are two entirely different things. You can replace MonoDevelop with any other text editor; it has nothing to do with compilation errors in Unity.

    --Eric
     
    ericmartinez likes this.
  5. jackson_31

    jackson_31

    Joined:
    Sep 20, 2014
    Posts:
    84
    All i want to do is use writetobinaryfile and readfrombinaryfile to write and read a list of integers.

    the error is in line
    Code (CSharp):
    1.  WriteToBinaryFile<SomeClass>("C:\\someClass.txt",ff);
     
  6. jackson_31

    jackson_31

    Joined:
    Sep 20, 2014
    Posts:
    84
    ok I found the solution.
    I just changed line 40 and 41 to this and it works
    Code (CSharp):
    1.         WriteToBinaryFile<List<SomeClass>>("C:\\someClass.txt",ff);
    2.         List<SomeClass>  gg = MyBinary.ReadFromBinaryFile<List<SomeClass>>("C:\\someClass.txt");