Godot Version
4.6.stable
Question
I'm trying to catch a button's "pressed" signal in a for-loop that iterates over a uncertain amount of buttons. In that loop, I would like to test for the button.pressed signal, but from all my testing, that returns true no matter what. The buttons are all children of a Node2D, in case that matters. I've also tried Toggle mode, action modes, and connecting the signal to a function, but nothing worked. (The ladder kind of did, but it always triggered the press of the first button, and not the one that was actually pressed. Is there a way that I can give the button's position as an argument in the "pressed" signal?)
My current code (doesnt work as intended):
for btn in get_children():
if btn.pressed:
Actions that get executed no matter what, even if no button pressed
Thank you for your help!
(Some text got formatted in a weird way, I dont know how to undo that, just ignore
)
Could you tell us the use case for this? This seems unnecessarily complicated.
I’m trying to make a fast travel system (like pylons in terraria for example) and therefore have to manage an unknown amount of buttons on the minimap, since the player can place as many “pylons” as they want
but yes, im sure theres simpler ways to do this…
Ahh, I get you now.
What you should do instead, in my opinion, is create a custom button that inherits the Button class.
Add your own custom signal, which can have a specific node / name / whatever you wish.
See: Using signals — Godot Engine (stable) documentation in English
In your own button, you can simply listen to the button’s pressed event, and when it happens, fire your own special event and give the data you need back.
There are examples in that link on how you can have a signal that can pass custom data.
In the script file where you need to listen to these buttons, you can do something like this (This is not exact code so don’t just blindly copy it please, it’s just for an example)
for child in get_children(): # Or where your buttons are
if child is CustomButton:
child.custom_pressed.connect(fastTravelButtonPressed)
func fastTravelButtonPressed(mapName: String) -> void:
print(mapName)
Thank you!
I also had this method in the back of my head, but didnt think it would be viable here.
Thanks a lot for changing my mind 
1 Like