ItemList and detecting item selection

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

I am trying to use an ItemList to create a clickable text menu. I could not find any tutorial on the subject, nor easy and clear explanation, so I really expected it to be as simple a using a list of buttons, but I think I was wrong.
My ItemList has two items: item01 and item02 and was set to send an item_selected(index int) signal

I am trying to get at least a print command working after item01 or item02 gets selected.
So far I tried things similar to:

func _on_ItemList_item_selected(index):
     if $ItemList.is_selected(1) = true:
          print("Item 01 selected")

but nothing works.

So, I have two questions.

  1. How to detect that item01 is selected/clicked in
    func _on_ItemList_item_selected(index):?
  2. Why is it so different from the idea of button pressing detection etc. (at least it looks like that at first)? :wink:

Your example code has a missing = in the condition. It has if value1 = value2 instead of if value1 == value2

Rodrigo Matos | 2020-04-03 17:32

Thanks Rodrigo Matos. You are right. That was the problem!

Freeman | 2020-04-03 17:41

:bust_in_silhouette: Reply From: njamster

I cannot reproduce your problem, this works fine:

extends ItemList

func _ready():
	add_item("item01")
	add_item("item02")

func _on_ItemList_item_selected(index):
	 if is_selected(0):
		  print("item01 selected")
	 if is_selected(1):
		  print("item02 selected")

Can you provide an example project with the problem?

Thanks for the answer.
My problem was the typo I did and = instead of ==, so Rodrigos Matos who commented was right and actually gave me the answer as well.

Freeman | 2020-04-03 21:03