Help! Deep Clone Class?

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.

public class Level
{
    public string level_name = "";
    public int ID = 0;
}

public class GameData
{
    public Level galaxy;
}

[ imagine a database Scriptable Object, holding a List<Galaxy> galaxies]

[ Imagine galaxies[0] has been setup with data, by me manually via the Inspector ]

public class Initialiser
{
    public GameData gameData

    public GetLevelDoOtherStuffAndSendToGameController()
     {
     gamedata.level = database.levels[0];

     GameController.Setup_Level( gameData )
     }
 
}

[ Imagine a method calling GameController.Setup_Galaxy( gameData ) ]

public class GameController : Monobehaviour
{

    public Setup_Level( GameData gameData)
    {
        blah...
    
        [B]Level[/B] [B]level_actual_seperate = gameData.level;[/B]
    
        blah...
    }

}

How can I do this [ level_actual_seperate = gameData.level; ] without passing the reference?

ANY help would be so welcome.

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:

public class Level {
   public string level_name = "";
   public int ID = 0;

   public Level Clone() => new Level {
      level_name = this.level_name,
      ID = this.ID
   };
}

Then simply call the Clone method to instantiate a new Level:

level_actual_seperate = gameData.level.Clone();

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.

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

1 Like

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.

I found a perfect solution here, using BinaryFormatting to create Deep Copies easily :
( Works perfectly )

https://stackoverflow.com/questions/78536/deep-cloning-objects/78568#78568