Making different buttons with one function do different things

Godot Version

4.2

Question

i am making a list of cards in my game.


image
theyre just made up of buttons and a label. what i wanted to do was when i press one button it plays a function. all the buttons would do is this
image
so i would have to make 24 buttons each one just saying card_stats_display(0) then (1) and (2) and so on until 23. i dont want to do this 24 times so i was wondering if there was a way i could make it so that all the buttons lead to one function that called the function but it would give their corresponding number when i pressed the button. im not sure if this sounds confusing. i can give extra context if needed.
for example something like
func _on_button_pressed(number):
card_stats_display(number)

just not sure how i would make the buttons give different numbers?

this is fairly easy, just do something like this:

func _ready() -> void:
    for button in all_buttons.size():
        all_buttons[button].pressed.connect(_on_button_ressed.bind(button))

This binds the paramter “button” which is the number 0 to 24 in your case to the method

1 Like

This makes sense but im not sure what the all_buttons is supposed to be. I tried the parent of the buttons and that didnt work so what would the all_buttons variable be in my case

all_buttons is a list of all your buttons. If the buttons are children of one node you can just use get_children()

1 Like

Okay i had a hard time getting to work but now to works fine tysm

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.