Select multiple items in a 'multi_select' ItemList dynamically

Godot Version

4.2.2

Question

Hello. I have a multi list ItemList which I am trying to dynamically select the items that were selected in the last session. I have saved the state of the items in a json. This works fine and the loading of the json.

I’m having difficulty with the code that selects the items. It ends up just highlighting the last item in the json in the iteration.

This the method that selects the items in the list

public static void LoadSelectionState()
        {
            var SaveFilePath = OpsDashboard.workItemTypeSaveFilePath;
            string globalPath = ProjectSettings.GlobalizePath(SaveFilePath);
            int workItemTypeListCount = OpsDashboard._workItemTypeList.ItemCount;
  
            OpsDashboard._workItemTypeList.SelectMode = ItemList.SelectModeEnum.Multi;

            if (!File.Exists(globalPath))
                return;

            var json = File.ReadAllText(globalPath);
            var selectedItems = JsonSerializer.Deserialize<List<string>>(json);
            
            if (selectedItems == null)
                return;

            for (int i = 0; i < workItemTypeListCount; i++)
            {
                if (selectedItems.Contains(OpsDashboard._workItemTypeList.GetItemText(i)))
                {
                    OpsDashboard._workItemTypeList.Select(i);
                }

            }
        }

Any help would be appreciated.

Thanks.

void select ( int idx, bool single=true )

As you can see from the docs, the select method takes a second parameter that, if not set, automatically says its the only one selected. call Select(i, false)

Hi there,

Thanks for the reply. I did see that, and tried it. It didn’t work for me so I removed it.

addendum:
I struggled to understand the context what 'bool single=true ’ actually meant. image
It’s rather obvious in hindsight. I put that down to being new to this.

It now works with the bool after some code adjustment. Thanks for confirming.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.