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?
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:
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.