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

C# Select All Variables

Discussion in 'Scripting' started by sherlockturtle, Feb 7, 2015.

  1. sherlockturtle

    sherlockturtle

    Joined:
    Jan 23, 2012
    Posts:
    592
    I know the title is a bit vague, but I can explain a bit better here. I have a large script with many many variables on it. Some of these are custom classes some of them are not. I am saving the data through SerializationInfo. I simply make a new variable with the same class as the script and copy the script to it.

    Code (csharp):
    1.  
    2. public ScriptName SavedData;
    3. SavedData = this;
    4.  
    That works fine, but then when I want to load it obviously the this keyword is read only so I cannot set it.

    Code (csharp):
    1.  
    2. this = read only;
    3.  
    Is there anyway somehow without putting all my variables into an array or making my script into a class to set all the variables in my script to the saved one without hard coding every single variable to be set?

    Thank you!

    Tim
     
  2. tango209

    tango209

    Joined:
    Feb 23, 2011
    Posts:
    379
    Typically, you would use another class to manage serialization.

    It would help if you had a more complete example showing the serialization calls.
     
  3. sherlockturtle

    sherlockturtle

    Joined:
    Jan 23, 2012
    Posts:
    592
    I do have another class that manages the saving and loading, but my issue is when I want to call on that class to load the data I want to simply be able to set all my current script's variables to that saved data, but I do not want to have to go through manually and say every variable. Is that possible?

    The Save File
    Code (csharp):
    1.  
    2. public TheScript AllSaveData;
    3.     public SaveData(SerializationInfo info, StreamingContext ctxt)
    4.     {
    5.         AllSaveData = (TheScript)info.GetValue("TheScript", typeof(TheScript));
    6.     }
    7.  
    Then when loading from "TheScript"
    Code (csharp):
    1.  
    2.     public void Load()
    3.     {
    4.         SaveData data = new SaveData();
    5.         Stream stream = File.Open("Save", FileMode.Open);
    6.         BinaryFormatter bformatter = new BinaryFormatter();
    7.         data = (SaveData)bformatter.Deserialize(stream);
    8.  
    9.     >>>    this = data.AllSaveData;
    10.  
    11.         stream.Close();
    12.     }
    13.  
    I understand this is readonly, but I am wondering if I can simply set all the variable values in my script to the variable values on data.AllSaveData.

    Is the easiest way to just make all my variables in one giant array? I feel like there has to be an easier method though.


    Thank you!
     
  4. tango209

    tango209

    Joined:
    Feb 23, 2011
    Posts:
    379
    Still not that clear. What classes are each of those scripts in? Is Load() in TheScript class? Is SaveData() in the SaveData class? Are the classes MonoBehaviours or just plain old C# classes?

    I'm trying to understand why you would need to deserialize a class in itself and try to assign that to itself? Does the class not have something that it belongs to? Is it your top level manager class?