Trouble connecting signal from instanced button

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

onready var grid = $GridContainer
var inventory_slot = preload("res://dev/inventory/inventory_slot.tscn")


#POPULATE INVENTORY
func _ready():
	for n in SaveAndLoad.runtime_data.building.keys():
		var inventory_slot_new = inventory_slot.instance() 
		if SaveAndLoad.runtime_data.building[n] == false: 
			var item_name = SaveAndLoad.runtime_data.building[n] 
			var icon_texture = load(Links.house_link[n]) 
			inventory_slot_new.set_button_icon(icon_texture) 
			inventory_slot_new.connect("id_pressed" , self, "_button_pressed") 
			grid.add_child(inventory_slot_new, true) 
			

For each key in my dictionary → instance scene (button) → if key is false → declare item name from dict → declare texture from dict → apply texture to button → connect button to function (This part doesn’t work) → add button to grid container

Hey all, this is driving me crazy. I can’t seem to connect my generated buttons to the _button_pressed function in my script. It has something to do with me instancing a button as a scene, since I can create buttons & connect them in a separate case.

inventory_slot_new.connect("id_pressed" , self, "_button_pressed")  

I don’t get an error message either. When I click one of the generated buttons in game nothing happens.

:bust_in_silhouette: Reply From: matthewjw

Two problems with my code, thank you to u/snikurtv for diagnosing the first, and prompting me to discover the second issue:

  • a regular button’s pressed signal is “pressed” not “id_pressed”
  • I needed to parse the new button through the .connect() function
inventory_slot_new.connect("pressed" , self, "_button_pressed", [inventory_slow_new])