Hello there,
I’ve spent pretty much the last two hours on this, excluding the amount of time I spent brainstorming and testing before leading up to this solution.
Basically, I’m still working on a saving and loading system, but I realized I have to split my data into two parts: permanent data and temporary stored data. I can easily get the temporary stored data (or so I think…), but my problem is getting the permanent data.
What I did was create a base class, create an instance of that class (or is it called an object?) that has all the data filled in for that level or such, and then I am attempting to get that filled in class by name. However, this is done by typing in the name of the class’s instance (like “W1L1” - a relatively simple system) so I can get the matching instance’s data.
However, I may end up with a ton of these instances (my levels alone would be the majority, but still, they’d be a ton). And so, rather than a million if or switch statements to compare that string to pre-defined names (again- each instance has its own name value for quick output purposes). I’ve come upon System.Reflections (Type.GetType(name)), but that doesn’t seem to work.
Any suggestions? Please feel free to ask for clarifications, because I have no idea how well I described this. Also, here are primary snippets of my code:
(Also, please note that I basically learned and am learning C# by myself as I go. Took quite a bit to figure out all at once classes, “instances” (or objects?), static objects/instances/variables (rather than static classes), enums, and such. My terminology may thus be quite a bit off. And it’s public static on purpose so I can very easily access the data- which may not make sense now that I think about it, since those are just reference instances that won’t be used. Rather, the copy instances/variables will be used globally… I may have wrote this last sentence wrong)
public class Control : MonoBehaviour {
public LevelType thisType; // for choosing enum, for a quick switch statement. Very general script
// ignore the above line if you wish, but it basically defines if the current scene is a level select, world select, or inside a level
public string levelIDInput;
// Input from Inspector, the above is how the instance is matched and then assigned... theoretically
public enum LevelType{
Other,
WorldSelect,
LevelSelect,
Game
}
// Next two sets are the base classes
public class levelData{
public string worldID{ get; set;}
public string levelID{ get; set;}
public string levelName{ get; set;}
public int totalPickups{ get; set;}
public bool secretCheck{ get; set;}
}
public class worldData{
public string worldID{ get; set;}
public int levelsTotal{ get; set;}
public int secretsTotal{ get; set;}
}
// Next two sets are examples of instances, one of each
public static worldData W1 = new worldData{
worldID = "W1",
levelsTotal = 4,
secretsTotal = 0
};
public static levelData W1L1 = new levelData{
worldID = "W1",
levelID = "W1L1",
levelName = "The Beginning",
totalPickups = 6,
secretCheck = false
};
void Start () { // Uses enum to define what type of scene this is, then acts accordinly
if (thisType == LevelType.Other) {
print ("Undefined LevelType?");
}
if (thisType == LevelType.WorldSelect) {
print ("This is a world menu!");
// Some random testing removed
}
if (thisType == LevelType.LevelSelect){
print ("This is a level menu!");
}
if(thisType == LevelType.Game){
Type thisLevel = Type.GetType(levelIDInput);
// The above is what I tried, then I would get all the values
// of the class instance into more scene-wise permanent variables
// for a variety of uses, including in other programs
//Quite a bit of not-so-random testing removed
}
}
}
Edit: Added a bit to the code that was more or less necessary