How to scroll through items in an ItemList?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Diet Estus

I am trying to design an inventory menu and have been experimenting with different GUI elements in Godot 3.0.

I notice that if I create a scene consisting of a Container and three Buttons, Godot will automatically do input processing and change the focus of the buttons depending on whether I press up or down.

Is there a way to get this kind of automatic input processing for scrolling through items in an ItemList?

I notice that items don’t have a focus attribute. Rather, they can be selected or not. This leads me to believe that there is no automatic input processing I can use to scroll through them. Must I write my own input processing to allow a user to scroll through items? For example, using ItemList.select()?

:bust_in_silhouette: Reply From: gswashburn

So in order to get list to scroll like buttons, add a script to your list and copy this code in. It checks if mouse enters itemlist and selects item mouse touches. If you exit list with mouse, selection is removed. This should give you ability to scroll through list.

extends ItemList

var mouse_select
var last_index

func _ready():	
	select_mode = ItemList.SELECT_SINGLE

func _input(event):
	# Check ItemList
	if InputEventMouse and mouse_select == true:
		var itm = get_item_at_position(get_local_mouse_position(),false)
		select(itm)
		# Using select() doesn't trigger item_selected
		_on_ItemList_item_selected(itm)

	pass

func _on_ItemList_mouse_entered():
	mouse_select = true

func _on_ItemList_mouse_exited():
	mouse_select = false
	unselect(get_selected_items()[0])

func _on_ItemList_item_selected(index):
	if not index == last_index:
		print(index)
		last_index = index