Search code examples
c#arraysunity-game-enginepropertyinfo

How Can I Get The Value of an Item in an Array via Reflection


Skip to the third paragraph for the question.

Context: I'm creating a 2D spaceship game, and one of the game mechanics is routing power to the 7 different parts of the ship to suit the current situation you're in. There are presets that you can switch to at any time, called power configurations. I have the power configurations stored in 3 different arrays, like so:

int[] powerConfiguration1 = new int[7] {10,10,10,10,20,20,20};
int[] powerConfiguration2 = new int[7] {20,20,20,10,10,10,10};
int[] powerConfiguration3 = new int[7] {10,20,10,20,10,20,10};

When you switch configurations, it calls a method for doing so. The method makes some calculations to determine how long it will take to switch configurations. However, instead of making a switch statement and copy/pasting the code several times in the case of the player switching to any of the three configurations, I want to use PropertyInfo in System.Reflections to choose which property I need to pull values from.

Question: The problem is that I don't know how to get an item from an array. Here is what I have so far, where I'm attempting to determine how much power will need to be rerouted in total and adding it all to a variable. 0 is the index in the configuration at which I have decided to store the shield power. powerToShields is the current power being routed to the shields.

void switchConfiguration(int number) {
PropertyInfo powerConfiguration = GetType().GetProperty("powerConfiguration" + number);
int powerToReroute = 0;
powerToReroute += Mathf.Abs(powerToShields - powerConfiguration[0]);

Could someone please explain what I'm doing wrong and/or show me how to fix it? Or, is there a better way to do this?

EDIT 1: This is coded in C# (Unity).


Solution

  • Side thought

    I guess my first question is why not store the arrays in a list. So, instead of powerConfiguration1, powerConfiguration2, powerConfiguration3, why not just store a list of int[], so

    List<int[]> powerConfigurationList = new List<int[]>;
    powerConfigurationList.Add(new int[7] {10,10,10,10,20,20,20});
    powerConfigurationList.Add(new int[7] {20,20,20,10,10,10,10});
    powerConfigurationList.Add(new int[7] {10,20,10,20,10,20,10});
    

    That way you can get the item via:

    powerToReroute = (powerConfigurationList[number])[0]
    

    Answer to your question

    However, assuming that there is some good reason that you can't, and in order to answer your exact question, do the following:

    ...
    PropertyInfo powerConfiguration = GetType().GetProperty("powerConfiguration" + number); //this line is taken from your example above
    
    //then you need to do something like the below
    var value = (int[])powerConfiguration.GetValue(instanceThatHasTheProperty);
    int powerToReroute = 0;
    powerToReroute += Mathf.Abs(powerToShields - value[0]);
    

    From your code snippet, I see you have GetType().GetProperty("powerConfiguration" + number);. I'm not sure what the actual instance is that your getting that type from. So you need to replace instanceThatHasTheProperty in my above snippet, by whatever instance you're trying to get the property's value from.