• Community
  • Itemlist multi select without pressing CTRL key.

I need a way to multi select items in Itemlist by clicking on the items only without needing to press the CTRL or SHIFT keys, like this :
clicking on item1 then clicking on another item, this way without pressing CTRL or SHIFT keys,
Or,
Let the CTRL key always pressed so i can enforce the player to multi select.

I hope you understand me.

You could store the indices of the clicked items in an array. And then loop through your array and set these indices as set.

    trizZzle
    Okay this a tricky one..
    But if there is any way to make CTRL key always clicked by a function.. this will be better for me.

      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)