Select from check buttons

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

I am trying to generate a Container that displays a bunch of items as Check Buttons, so that those that are selected get their text appended to a list. Here is my code:


var items = ["Item 1", "Item 2", "Item 3"]
var selected_items = []

func _ready():
	var vbox = VBoxContainer.new()
	add_child($vbox)	
	for item in items:
		var check_button = CheckButton.new()
		check_button.text = item
		check_button.connect("toggled", self, "_on_item_toggled", [item])
		$vbox.add_child(check_button)
	
func _on_item_toggled(item, value):
	if value:
		selected_items.append(item)
	else:
		selected_items.erase(item)
	print("Selected items: ", selected_items)

However what gets printed is
Selected items: [True, True, True]
rather than the actual items.
Thanks for any help
mimmo

:bust_in_silhouette: Reply From: mimmo970

OK, I figured out what the problem is: in the on item toggled function, item and value were exchanged.
This here works:

func _on_item_toggled(item, value):
	if item:
		selected_items.append(value)
	else:
		selected_items.erase(value)

	print("Selected items:", selected_items)

Hope this may be useful for someone else.