Home > Net >  How to store an organised list of constants in C#
How to store an organised list of constants in C#

Time:07-25

I want to store fixed settings for each game level. I want it to be structured like this:

namespace LevelSettings
{
    namespace Bar
    {
        public static int ExtraActiveBlocks = 3;

        "Level1"
        {   public static float Speed = 0.6f;
            public static string ActiveColor = "red";
            public static string InactiveColor = "black";
        },
        "Level2"
        {
            public static float Speed = 0.6f;
            public static string ActiveColor = "red";
            public static string InactiveColor = "black";
        },
        "Level3"
        {
            public static float Speed = 0.6f;
            public static string ActiveColor = "red";
            public static string InactiveColor = "black";
        }

This is obviously not valid C# code. However I'm asking how to code it in C#.

So in my C# (Unity) script I want to access the fields like this:

str currentLevel = "Level1"
float currentSpeed = LevelSettings.Bar.{currentLevel}.Speed;

Is this storage and access functionality achievable in C#? If so, how can the above be done?

CodePudding user response:

with a static dictionary inside your constants where the key is the level, and the value is an object representing the 3 properties.

namespace LevelSettings
{
    public class LevelSetting
    {
       public float Speed {get;}
       public string ActiveColor{get;}
       public string InactiveColor{get;}
    
       public LevelSetting(float speed, string activeColor, string inactiveColor)
       {
           /// set the properties, omitted for brevity
       }
    }

    class Bar
    {
        public static int ExtraActiveBlocks = 3;
        public static IReadOnlyDictionary<string,LevelSetting> Levels = new Dictionary<string,LevelSetting>()
        {
           ["Level1"] = new LevelSetting(0.6f, "red","black"),
           ["Level2"] = new LevelSetting(0.6f, "red","black"),
           ["Level3"] = new LevelSetting(0.6f, "red","black")
        };
    }
}

You can then do

string currentLevel = "Level1"
float currentSpeed = LevelSettings.Bar.Levels[currentLevel].Speed;

CodePudding user response:

You could use nested classes to get multiple levels:

public static class Bar{
    public const  ExtraActiveBlocks = 3;
    public static class Level1{
          public const float Speed = 0.6f;
          public const string ActiveColor = "red";
          public const string InactiveColor = "black";
    }
}

If you want to index your properties you need to use a list or dictionary:

public static class Bar{
    public const  ExtraActiveBlocks = 3;
    public static readonly IReadOnlyList<Level> Levels = new List<Level>(){ 
new Level(){Speed = 0.6, ActiveColor = "red", InactiveColor = "black"}};

    public class Level{
          public float Speed {get; init;}
          public string ActiveColor {get; init;}
          public string InactiveColor {get; init;}
    }
}

Note that you should make your constants either const or static readonly. I would highly advice against using any mutable global state, since that will likely result in difficulty knowing what is accessing your properties.

  • Related