Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Resolved Help! Deep Clone Class?

Discussion in 'Scripting' started by Lapsapnow, Oct 14, 2020.

  1. Lapsapnow

    Lapsapnow

    Joined:
    Jul 4, 2019
    Posts:
    51
    Hello!

    I'm not great at C# yet...

    I have a class GameData, that contains variables intended to hold new separate instances of other class instances.

    For example, GameData gameData_current, contains Level level_current;

    I want to retrieve level_current 's data, NOT it's referance. How can I do that?

    I want to pass a premade class instance, from a list of classes in a Scriptable Object, into a new separate class object instance being used. But I have no idea how to do it without passing the reference instead.

    Code (CSharp):
    1.  
    2. public class Level
    3. {
    4.     public string level_name = "";
    5.     public int ID = 0;
    6. }
    7.  
    8. public class GameData
    9. {
    10.     public Level galaxy;
    11. }
    12.  
    13. [ imagine a database Scriptable Object, holding a List<Galaxy> galaxies]
    14.  
    15. [ Imagine galaxies[0] has been setup with data, by me manually via the Inspector ]
    16.  
    17. public class Initialiser
    18. {
    19.     public GameData gameData
    20.  
    21.     public GetLevelDoOtherStuffAndSendToGameController()
    22.      {
    23.      gamedata.level = database.levels[0];
    24.  
    25.      GameController.Setup_Level( gameData )
    26.      }
    27.  
    28. }
    29.  
    30. [ Imagine a method calling GameController.Setup_Galaxy( gameData ) ]
    31.  
    32. public class GameController : Monobehaviour
    33. {
    34.  
    35.     public Setup_Level( GameData gameData)
    36.     {
    37.         blah...
    38.    
    39.         [B]Level[/B] [B]level_actual_seperate = gameData.level;[/B]
    40.    
    41.         blah...
    42.     }
    43.  
    44. }
    45.  
    How can I do this [ level_actual_seperate = gameData.level; ] without passing the reference?

    ANY help would be so welcome.
     
    Last edited: Oct 14, 2020
  2. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    You'll need to instantiate a new Level object with the same values.

    One simple of doing that is by adding a method inside Level that clones itself:
    Code (CSharp):
    1. public class Level {
    2.    public string level_name = "";
    3.    public int ID = 0;
    4.  
    5.    public Level Clone() => new Level {
    6.       level_name = this.level_name,
    7.       ID = this.ID
    8.    };
    9. }
    Then simply call the Clone method to instantiate a new Level:
    Code (CSharp):
    1. level_actual_seperate = gameData.level.Clone();
     
    Last edited: Oct 14, 2020
  3. Lapsapnow

    Lapsapnow

    Joined:
    Jul 4, 2019
    Posts:
    51
    Thank you Vryken!
    I was just hoping there was some easier dynamic way I was not aware of.

    Also, if possible.
    How would you assign a class object to a variable by value, not referance? As in, byVal not byRef
    As in, how can I setup GameData to deep copy Level not take a referance?

    Since... Level contains a - lot - of information. It also contains referances to other sub classes that are also referenced to other places. So, it seems I'll have to this for a lot of things... how painful.
     
  4. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    If it's applicable, you could turn Level into a struct instead of a class; structs are value types and wouldn't need to be cloned when accessed. If Level's only purpose is to contain data, then that should work.

    Other than that, there's no built-in way to access a class by value. You might be able to find some deep-copy packages on the asset store, or you could try something like the responses in this thread:
    https://stackoverflow.com/questions/129389/how-do-you-do-a-deep-copy-of-an-object-in-net/11308879
     
    Last edited: Oct 14, 2020
    Lapsapnow likes this.
  5. Lapsapnow

    Lapsapnow

    Joined:
    Jul 4, 2019
    Posts:
    51
    Thank you very much Vryken.

    Ah, Structs. Good ideah. Sadly though Scriptable Objects don't seem to be able to work with them... But I might be wrong.

    Thanks none the less. Go well.
     
  6. Lapsapnow

    Lapsapnow

    Joined:
    Jul 4, 2019
    Posts:
    51