Add MinItems and MaxItems properties to ListDrawerSettingsAttribute

Issue #829 new
Dan Vicarel created an issue

It would be really helpful if one could specify the min and max number of elements allowed for a list. To make this general, I would think ListDrawerSettingsAttribute should expose new MinItems and MaxItems properties. I tried making this work with the current APIs (v3.0.12.0 on Windows 11 Pro) via CustomAddFunction and CustomRemoveFunction, but these properties are poorly documented so I wasn’t really sure how to use them, and I think I’m running into issue #437 anyway.

Comments (3)

  1. Antonio Rafael Antunes Miranda

    If you’re using the TableList attribute, then yes, you won’t be able to use the ListDrawerSettings attribute since they’re not compatible.


    The CustomAddFunction and CustomRemoveElementFunction / CustomRemoveIndexFunction work like this:

    • Inside the ListDrawerSettings attribute you provide the name of the respective function as a string.
    • When Odin detects that you want to add or remove an element, it will call the respective function and pass it the arguments that you’ve specified. Valid arguments are:

      • CustomAddFunction: InspectorProperty property, List<T> list where T will be the type your list holds.
      • CustomRemoveIndexFunction: InspectorProperty property, List<T> list, int index where T will be the type your list holds.
      • CustomRemoveElementFunction: InspectorProperty property, List<T> list, T elementToBeRemoved where T will be the type your list holds.

    Example:

    // Remove by Index
    [ListDrawerSettings(CustomAddFunction = "CustomAdd", CustomRemoveIndexFunction = "CustomRemoveIndex")]
    public List<string> SomeListOfStrings1 = new List<string>();
    
    // Remove by element
    [ListDrawerSettings(CustomAddFunction = "CustomAdd", CustomRemoveElementFunction = "CustomRemoveElement")]
    public List<string> SomeListOfStrings2 = new List<string>();
    
    // We don't need the InspectorProperty so we don't add it to the parameter list.
    private void CustomAdd(List<string> list)
    {
        list.Add("I was added by a custom add function.");
    }
    
    //We don't need the InspectorProperty so we don't add it to the parameter list
    private void CustomRemoveIndex(List<string> list, int index)
    {
        list.RemoveAt(index);
    }
    
    //We don't need the InspectorProperty so we don't add it to the parameter list
    private void CustomRemoveElement(List<string> list, string elementToBeRemoved)
    {
        list.Remove(elementToBeRemoved);
    }
    

  2. Dan Vicarel reporter

    Antonio, thanks for description, that was very helpful! All of this seems like information that should be in the docs….maybe I missed it. Is there a page other than this one that already had this info?

  3. Antonio Rafael Antunes Miranda

    The documentation around resolvers is a bit lacking right now, but there are a few things that can help with them.

    • You always have access to the InspectorProperty and the value so those are fixed. That information is listed here: https://odininspector.com/tutorials/value-and-action-resolvers/named-values
    • You can define a parameter that you know/think is not part of the named values and then you will get an error message in the inspector that shows you all possible values and their types.

    I understand that this is by no means perfect, but other things kind of took the attention 😅 If you have questions about something then the discord is a great place to ask as it’s usuall easier to see and answer there.


    I’ve written down all your suggestions from this and the other ticket and I’m going to write down something for the docs that will then hopefully be available there.

  4. Log in to comment