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

This subforum contains all the issues which we already resolved.
Post Reply
kubel34
Burner Inserter
Burner Inserter
Posts: 5
Joined: Mon Aug 26, 2019 5:48 pm
Contact:

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

Post 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.

posila
Factorio Staff
Factorio Staff
Posts: 5147
Joined: Thu Jun 11, 2015 1:35 pm
Contact:

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

Post 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)

Post Reply

Return to “Resolved Problems and Bugs”