Connecting signals not working

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

I have a grid of a lot of buttons, and I’m trying something like this

var button_array = $container.get_children()
for button in range(20):
    button_array[button].connect("pressed", self, "function", [button])

func function(button : Button)
    button.text = "clicked"

This is not working, and I have no idea why.

:bust_in_silhouette: Reply From: kidscancode

When you added [button] at the end, that’s using the loop index, which is an integer.

A loop index isn’t even needed. Try this for much cleaner code:

for button in $container.get_children()
    button.connect("pressed", self, "function", [button])

func function(button : Button)
    button.text = "clicked"

Ah, that would be better. Unfortunately, I think the problem is much more deep rooted. I’ve tried connecting one of the buttons with the editor to a function that prints “test”, and even that doesn’t work. I’m genuinely lost at the minute, I have absolutely no idea why even that doesn’t work.

psear | 2020-06-15 15:25

Well, that means it’s something else. Are you sure the button is detecting the click? There isn’t another control overlaying it that could be stealing the input?

You say you have a grid of buttons. Is that a bunch of buttons in a GridContainer? What else is in the scene?

kidscancode | 2020-06-15 15:27

It is a bunch of other buttons in a grid, I think I haven’t quite understood how UI elements work. It’s like this:
Grid with some buttons
Control node that holds a bunch of Line2Ds that draw a grid (no control units inside of it)
Control node that holds a bunch of Line2Ds that draw a grid (no control units inside of it)
Another Grid that holds more buttons
A small panel with a final button at the bottom

I’ve got it sort of working at the minute. As it turns out, the two control nodes holding only Line2Ds were blocking clicks on the grid, and it’s working if I put the grid below them in the tree.

It never occurred to me as there are no buttons or anything, just an empty control node which was positioned in a way that let me use anchors to get points to set out the grid.

Thanks for pointing it out, though I did feel it was a bit unintuitive that one control node would block another even when there was nothing in it, though I guess it makes sense.

psear | 2020-06-15 17:33