Button connect parameters dont work

Godot Version

v4.2.1.stable.official

Question

edit: sorry for the formatting idk why it gets rid of the indents here
Okay so I’ve googled for way too long and even tried to get an answer from ChatGPT but literally nothing helps…
I am creating a new button through a script and want to connect the newly instanciated button to a “pressed” function including parameters. When doing so it tells me that either it wants only integers (I have to use strings) or that it is for some reason not callable?
The button itself works, I’ve tried it with a simple print().
Please help me, I am really freaking out because of this.

My code:
inventory.gd:
extends Control

var itemsLoad = [
“res://!/Objects/Items/ItemsRessources/big_fucking_sword.tres”
]
func _ready():
for i in itemsLoad.size():
var item := InventoryItem.new()
var itemData = load(itemsLoad[i])
item.init(itemData, itemData.name) # Pass the item name to init()
%inventory_box.add_child(item)
# Apply the UI theme to the button
item.set_theme(load(“res://!/UI/Themes/InventoryItemName.tres”))

#this next line is where it doesnt work

item.connect(“pressed”,self.__on_item_button_pressed, itemData)

func _on_item_button_pressed(itemData):
var itemName = itemData # Get the item name from the item data
%details_name.text = itemName # Update the label with the item name
print(“Pressed”)
func _process(delta):
pass

inventoryitem.gd:
class_name InventoryItem
extends Button
@export var data: ItemData
func init(d: ItemData, bt: String) → void:
data = d
text = bt

func _ready():
alignment = HORIZONTAL_ALIGNMENT_LEFT

You can use Callable and bind for this:

func _ready() -> void:
	$Button.pressed.connect(Callable(_on_my).bind("42"))


func _on_my(txt: String):
	print ("foo ", txt)

So in your case I would guess something like this:

item.pressed.connect(Callable(self, "_on_item_button_pressed").bind(itemData))

For future reference, you can use the “</>”-button above the text-entry field for creating code-blocks that keep the indents. Alternatively put 3 backtick characters before and 3 backtick characters after your code.

2 Likes

Thank you so much!