Page 1 of 1

[posila][0.17.66] Listbox "selected_index" not updated programatically

Posted: Thu Aug 29, 2019 12:17 pm
by kubel34
The value "selected_index" of a listbox never updates when calling methods that modifies the item list, even "clear_items".

Steps to reproduce:
- Display a list with items

Code: Select all

/c game.player.gui.center.add{type="list-box", name="list", items={"a", "b", "c"}}
- Select the last item
- Print the selected item index

Code: Select all

/c game.print(game.player.gui.center["list"].selected_index)
- Remove the last item

Code: Select all

/c game.player.gui.center["list"].remove_item(3)
I expected the index to change to the last item if you removed the last item, either by becoming nil, 0 or by selecting the new last item, but index does not change at all. This can be problematic as the index is now pointing to a non-existant item in the list.
- Clear the list

Code: Select all

/c game.player.gui.center["list"].clear_items()
The selected index is still the same as before. You have now a non-zero index in an empty list.
- Fill the list again

Code: Select all

/c game.player.gui.center["list"].add_item("d"); game.player.gui.center["list"].add_item("e"); game.player.gui.center["list"].add_item("f")
The selected index still didn't changed, but now the item does not appear selected in the list.
This also happen when you insert an item before the selected item : the highlighted item is not the one the selected index is pointing to.

Re: [posila][0.17.66] Listbox "selected_index" not updated programatically

Posted: Fri Aug 30, 2019 1:08 pm
by posila
Thanks for the report.
Fixed for 0.17.66

The item that you selected will stay selected if you add or remove other items (so selected_index will be adjusted if needed). If you remove selected item, selected_index will become 0 (no item is selected)

If you change items by writing to LuaGuiElement::items, selected_index will be preserved if count of new items allows it. (I am still not sure if this is good behavior, though)