GodotHasan
You can do it like this:
extends ItemList
var selected_indices : Array[int] = []
func _ready():
# Connect signal when selection happend (On item list set select mode to "Multi", else signal won't fire)
multi_selected.connect(update_selection)
# put some items in it for testing
for i in 42:
add_item("Item no. %s" %i)
# Second parameter is required for handling the signal, but not needed doing it this way.
func update_selection(index : int, selected : bool):
# We need to deselect all, else the last deselected item gets deselected only the next time we select/deselect something
deselect_all()
# Remove index if is selected
if selected_indices.has(index):
selected_indices.erase(index)
# Add index if was not selected
else:
selected_indices.append(index)
# Select all indices we stored
for i in selected_indices:
select(i, false) # Parameter 2 needs to be false to allow selecting multiple values
print(get_selected_items())
The next one would be much shorter, but does not work. Calling select, deselects everything else here I guess. But not in the above example. In the above example you also don't see a border on the first selected item. Maybe someone knows why this is?
func update_selection(index : int, selected : bool):
if selected:
select(index, false)
else:
deselect(index)