I'm using reflection to create instances of all classes derived from ItemPart as follows:
//Create UI from reflection
private void DisplayParts(string partGroup)
{
List<ItemPart> validParts = GetPartsList(partGroup);
foreach(ItemPart part in validParts)
{
GameObject obj = Instantiate(UIController.ObjectPrefabs[UIController.ObjectPrefabsEnum.ButtonDescription], UI_PartsList.transform);
DescriptionButton button = obj.GetComponent<DescriptionButton>();
button.Title.text = part.PartName;
button.Description.text = part.Description;
button.ActivateAction = delegate { DesignBench.CreatePart(part); };
}
}
//Get classes derived from ItemPart and return a list of instances
private List<ItemPart> GetPartsList(string partGroup)
{
List<Type> types = AppDomain.CurrentDomain.GetAssemblies().SelectMany(s => s.GetTypes()).Where(p => typeof(ItemPart).IsAssignableFrom(p) && p != typeof(ItemPart) ).ToList();
List<ItemPart> validParts = new List<ItemPart>();
foreach(Type type in types)
{
ItemPart part = (ItemPart)Activator.CreateInstance(type);
if (part.PartGroup == partGroup)
{
validParts.Add(part);
}
}
return validParts;
}
After accessing variables in this class, I've found while the IDE is claiming they are correctly at their initialised values, they are reading by the program as being 0:
public class Handle : ItemPart
{
//...
public new PartModifiableStats ModifiableStats = new PartModifiableStats()
{
SizeX = new ModifiableStat<float>() { IsEnabled = false, Value = 5 },
SizeY = new ModifiableStat<float>() { IsEnabled = true, Value = 20, Bounds = new Vector2(10, 150) },
Material = new ModifiableStat<Materials.Material>() { IsEnabled = true, Value = Materials.MaterialDict[Materials.MaterialTypes.Iron] }
};
//...
}
https://puu.sh/GcS8h/9a3c26de2b.png https://puu.sh/GcS8L/4911fbd3d8.png
Supposedly, Activator.CreateInstance(type) uses the type's empty constructor, which if I'm not mistaken, should also properly initialise all the variables in the type it is creating. Why is this value still reading as zero, and why is there a discrepency between what the program is reading and what the IDE believes the value to be?
public struct ModifiableStat<T>
{
public bool IsEnabled;
public T Value;
public Vector2 Bounds;
}
public struct PartModifiableStats
{
public ModifiableStat<float> SizeX;
public ModifiableStat<float> SizeY;
public ModifiableStat<Materials.Material> Material;
}
Aucun commentaire:
Enregistrer un commentaire