Suggestion: constraining keys and values of string dictionaries to a string enumerable (illustrated)

Issue #561 open
Artyom Zuev created an issue

When working on a heavily data-driven game, I often have a need to manipulate string fields and collections of strings. Constraining string fields and lists to aid validation and prevent erroneous input is simple with an existing ValueDropdown attribute. It enables fields like “unit type” or lists like “part hardpoints” to be very easily extendable while keeping the editing experience fast and error-free.

However, dictionaries have no support for ValueDropdown - and furthermore, they could probably use a dictionary-specific pair of attributes, since dictionaries are a more complex case which ValueDropdown can’t fully cover. So, I’m proposing two new attributes: DictionaryKeyDropdown and DictionaryValueDropdown, illustrated below:

Comments (2)

  1. Bjarke Elias
    • changed status to open

    Thanks a lot for putting this together. It's about time we find a good way of assigning attributes to dictionary keys and values.

    Here is another suggestion to get the ball rolling:

    [AssignDictionaryKeyAttribute(typeof(ValueDropdownAttribute), arguments)] 
    [AssignDictionaryValueAttribute(typeof(ValueDropdownAttribute), arguments)] 
    public Dictionary<string, string> myStrLookup;
    
    // It is limited to constructors. So you would not be able to assign any members like so: [ValueDropdown(AppendNextDrawer = true)].
    

    Another idea would be to utilize the new Odin Attribute Expressions, and do something like this:

    [AssignDictionaryKeyAttribute("@new ValueDropdownAttribute(\"...\")")]
    [AssignDictionaryValueAttribute("@new ValueDropdownAttribute(\"...\")")]
    public Dictionary<string, string> myStrLookup;
    
    // Then almost anything would be possible, but does produce quite a bit of clutter.
    
  2. Artyom Zuev reporter

    I think the first idea could work well, I’m hesitant to go with attribute expressions there, especially if “…\” clarification is required to make it work. Either way, that means adding optional argument support for ValueDropdown that gets its value from target of an attribute, which I hope is feasible. So, e.g. ValueAttribute (UseTargetAsArgument = true) on a field of type T would seek a method with signature void (T arg) and would feed a value of that target field into the invoked method. This would be rarely used on plain fields and lists, but would be invaluable for dictionary-specific needs: the most important case of constraining a lookup dictionary is about presenting different dropdown options based on a key of a given entry, which means that implementation of a method returning a string collection for that value dropdown in a dictionary must have access to KeyValuePair or Key as an argument.

  3. Log in to comment