1

I am making a game. The movement of the player is a series of pre-defined stages. Because this principle is the backbone of my game and there are many NPCs at once, I try to make this as efficient as possible. So I want to include the values of each stage by using constants.

At certain events (like a pressed button for the player) the stage switches to another stage. Because I can't refer to another nested class in a constant, I additionally want to set up an array that contains references to each stage.

The names of the classes are written in enums, and I refer by their integer value.

How do I do that? I want to include the solution at the given position in the code.

If there is a better way to do that by the way, I appreciate if you forward me to it.

enum TYPE
{
    hum //and more
}

enum HUM
{
    standF, stand_jumpF, stand_kneeF, stand_wanderF //and more
}

static class CONST
{
    public static class hum
    {
        public static class standF
        {
            public const int duration = 1;
            public const int actionLeft = (int)HUM.stand_jumpF;
            public const int actionRight = (int)HUM.stand_kneeF;
            public const int actionFaster = (int)HUM.stand_wanderF;
        }
        //and more
    }
    //and more
}

public static class REF
{
    public static Type[][] CONST { get; }
    static REF()
    {
        Type[][] REF = new Type[Enum.GetNames(typeof(TYPE)).Length][];

        Type[TYPE.hum][] REF = new Type[Enum.GetNames(typeof(HUM)).Length];
        for (int i = 0; i < Enum.GetNames(typeof(HUM)).Length; i++)
        {
            Type[TYPE.hum][i] = //what to do here?
        }
    }
}

After reading comparable questions about non-nested class I would have used this solution:

Type[TYPE.hum][i] = Type.GetType(Enum.GetName(typeof(HUM), i));

But it doesn't seem to do the trick.

Urben
  • 65
  • 5
  • The name of the class isn't just `standF`, it is `yournamespace.CONST+hum+standF`, that's just the way these nested classes are named. So you need to mimic that when using `Type.GetType`. – Lasse V. Karlsen Oct 14 '19 at 09:16
  • The name of the class is CONST+hum, this cannot be represented with an enum name. This is a weired design with enums anyway. Use a List or an Array of Types and you have no problem. Why do you want to refer to Type names via String ? If you change your Type name, your entire application is broken. – Holger Oct 14 '19 at 09:18
  • @LasseVågsætherKarlsen Oh I see, I tried it with periods instead of + in another attempt. Thank you! – Urben Oct 14 '19 at 09:24
  • @Holger This is an Array of Types so I am not sure why you say it isn't? I do it with strings because I don't see how else to do it. Of course I am interested in other solutions. – Urben Oct 14 '19 at 09:27
  • You use the enum, to hold a list of constant strings (your first lines). This should already be an array of types in first place. instead of enum Hum, you can have Type[] Hum = new { typeof(Const.hum.standf), typeof(stand_jumpF) ...} Even using an enum to hold a list of strings is a strange approach. – Holger Oct 14 '19 at 09:31

1 Answers1

0

You are messing up with array notations. Have a look at following question for more information: Multidimensional Array [][] vs [,]

I would suggest working with a dictionary (although I find your whole design weird to say the least), you'll need to add using System.Collections.Generic; to your usings.

public static class REF
{
    public static Type[][] CONST { get; }
    static REF()
    {
        Dictionary<Enum, List<Enum>> REF = new Dictionary<Enum, List<Enum>>();
        REF.Add(TYPE.hum, new List<Enum>());
        foreach (var item in Enum.GetValues(typeof(HUM)))
        {
            REF[TYPE.hum].Add((HUM)item);
        }


        // Demo code, print all values in TYPE.hum
        foreach (var item in REF[TYPE.hum])
        {
            Console.WriteLine(item);
        }
    }
}
Stormenet
  • 25,926
  • 9
  • 53
  • 65
  • Weird in the sense of being uncommon or being problematic at other potential parts of the script? – Urben Oct 14 '19 at 09:29
  • 1
    You are mixing same names with just capital changes, some of which would interfere with c# keywords (eg, ref, const, Type) which is bad naming imo. – Stormenet Oct 14 '19 at 09:34