0 votes

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.

asked Jun 15 in Engine by psear (103 points)

1 Answer

0 votes

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"
answered Jun 15 by kidscancode (17,016 points)

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.

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?

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:
Control:
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.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.