Best practice for storing related constants?

In my game, I have an enumeration (about 200 entries) with different GameEntityType s.

When the game is saved, only the GameEntityType array is written to the save file.

To restore the game world and display various information about GameEntityTypes I need to have a list of a few more additional details (constant values). This means that each GameEntityType has other values ​​associated with it. These values ​​are available very often in each frame. My goal is to have an easy way to get all the additional information from the GameEntityTypeID (enumeration). For example, when I read 0x1 from the save file, I can just access the name and all the other information that is implied with a number / listing from an array like this: "Constants.TextureList [ GameEntityType ]", will return the string "Stone_Texture.DDS"

An example of additional / related information of the first listing entry:

Types: string , string, Flag-Enum: "Visual-Classification" , bool , bool Values: "StoneBlock" , "Stone_Texture.DDS" , 0x0102 , false , true

My first approach to this is to create a static GameEntityTypeInfo class, which for each additional type of information was a member like the following:

public static const string [] = {"StoneBlock", ... [more entrys] };

Of course, this is a terrible decision, because I cannot just add GameEntityType wherever I want, without updating all the other lists. I also have to separate the logical units in data type units (which is a problem! Because when I decide that I no longer need a specific EntityType, I need to go through more than 6 lists!)

Then I tried to solve this problem by creating a structure from these datasets and adding them to a static array. But creating a list with additional information, while the constructor (while the game starts) still does not seem to be the best solution.

Question: How to create fast (search by name / classification each frame will be used at least 5000 times) and easy access (preferably with indexers) to these constant values?

A search like this would be the best "Constants.TextureList [ GameEntityType ]"

+4
source share
2 answers

Subclassification:

 public abstract class Enumeration : IComparable { private readonly int _value; private readonly string _displayName; protected Enumeration() { } protected Enumeration(int value, string displayName) { _value = value; _displayName = displayName; } public int Value { get { return _value; } } public string DisplayName { get { return _displayName; } } public override string ToString() { return DisplayName; } public static IEnumerable<T> GetAll<T>() where T : Enumeration, new() { var type = typeof(T); var fields = type.GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.DeclaredOnly); foreach (var info in fields) { var instance = new T(); var locatedValue = info.GetValue(instance) as T; if (locatedValue != null) { yield return locatedValue; } } } public override bool Equals(object obj) { var otherValue = obj as Enumeration; if (otherValue == null) { return false; } var typeMatches = GetType().Equals(obj.GetType()); var valueMatches = _value.Equals(otherValue.Value); return typeMatches && valueMatches; } public override int GetHashCode() { return _value.GetHashCode(); } public static int AbsoluteDifference(Enumeration firstValue, Enumeration secondValue) { var absoluteDifference = Math.Abs(firstValue.Value - secondValue.Value); return absoluteDifference; } public static T FromValue<T>(int value) where T : Enumeration, new() { var matchingItem = parse<T, int>(value, "value", item => item.Value == value); return matchingItem; } public static T FromDisplayName<T>(string displayName) where T : Enumeration, new() { var matchingItem = parse<T, string>(displayName, "display name", item => item.DisplayName == displayName); return matchingItem; } private static T parse<T, K>(K value, string description, Func<T, bool> predicate) where T : Enumeration, new() { var matchingItem = GetAll<T>().FirstOrDefault(predicate); if (matchingItem == null) { var message = string.Format("'{0}' is not a valid {1} in {2}", value, description, typeof(T)); throw new ApplicationException(message); } return matchingItem; } public int CompareTo(object other) { return Value.CompareTo(((Enumeration)other).Value); } } 

Article here: http://www.lostechies.com/blogs/jimmy_bogard/archive/2008/08/12/enumeration-classes.aspx

+2
source

Not sure if I understand exactly what you are trying to get, but it looks like you are looking for Dictionary<string, List<string>> if you are after a quick search.

0
source

Source: https://habr.com/ru/post/1340258/


All Articles