I have a class that has A LOT of public variables and I need to be able to get a list of them.
Here's an example of my class:
public class FeatList: MonoBehaviour {
public static Feat Acrobatic = new Feat("Acrobatic", false, "");
public static Feat AgileManeuvers = new Feat("Agile Maneuvers", false, "" ); void Start(){}}
Except there are about 100 more variables. Is there any possible way to get all these member variables in one manageable array? Or have I screwed myself over?
If by "Variables" you mean class fields (like variables at the class level) you can use reflection to get access, like in this MSDN Microsoft example using the FieldInfo class (see MSDN link for more info)
Instead of querying the
FieldInfoClass
in this example from the Main method you can choose yourFeatList
class. The logic does not need to be in a main method of the same class. You can place your version of the logic external to the entity you want to query and in fact query any object or class with this kind of logic.It doesn't matter if the fields are private or public or something else - through reflection you can get access to all of them.
See the MSDN sample code at FieldInfo.GetValue(..) method (MSDN link) for how to extract the field's value using reflection.
if you're after the varaible NAMES - then this will give them to you:
but if you want the VALUES, then this will work:
This will returns an FieldInfo array of all public fields of Feat type:
Then you may read/write fields like this:
Of cource, GetValue returns untyped object, so you need to cast it to the correct type on demand.