Why do my buttons only work with certain types of containers?

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

In my scene tree, I have these buttons that I add at runtime via code. They are a packed scene made of a button node with an animated sprite attached to it. I’m adding 20 of them and I want them to be arranged in a grid layout.

The buttons by themselves work fine, and they also work fine if I add them as children of any type of control node (they are added as intended, they receive input as intended). But for some reason, they stop working when they are children of certain type of containers : this includes GridContainer and HBoxContainer, although they work with a VBoxContainer or a basic Control node. They are added as expected but they can’t receive mouse input anymore.

I’ve tried every mouse filter, but the buttons never receive the mouse input when they are children of a GridContainer or HBoxContainer

I have no idea what’s causing this, and after 2 weeks of trying, I’m out of ideas to try…
I used the debugger to check what’s receiving the mouse input : when mouse filter is set to “pass” on the grid container, the grid container will receive the input (not the buttons), and when it’s set to “ignore”, the input will not be handled (the debugger will display the last control clicked like the input wasn’t handled at all). The buttons’ mouse filter is always set to “stop”.

So I could just use a Vboxcontainer but I need them to be arranged in a grid layout and just using right click and the parent’s node to change its type make the button unclickable again

After more testing, I found out the buttons work with every type of container as parent except four of them : GridContainer, HBoxContainer, CenterContainer and ScrollContainer. (Just using right click again to change parent node type, not changing anything else in the scene). With every other container, the buttons work as intended. What is the logic behind that?

Thank you to anyone who could help me with this

:bust_in_silhouette: Reply From: old_el_paso

Hey, so this post is almost a year old, but I came across it when also trying to solve this issue. In my case, I figured out what the issue is, so I figured I’d post my experiences here in case someone else in the future comes across this post trying to find a solution like I did. For the record, I am using Godot 4, not 3.5; however, I don’t believe there’s anything uniquely 4.0 about this, so perhaps this will also solve the issue you’re having.

Basically, it appears to come down to, at least in my case, how containers resize buttons. I was also adding buttons to an HBox at runtime based off of a scene, and while I had set the size of the button on the scene, a little poking around the node tree while my game was running showed that for some reason, when the buttons were being instanced, their size was being shrunk to 8x8. Everything else seemed to be fine, so visually it appeared okay (for example, in your case, if this is what’s happening, the animated sprite would still appear fine, as that’s not getting resized; just the parent button node). Nonetheless, the clickable area was being shrunk to 8x8; sure enough, when discovering this, I started clicking the buttons snug in the top left corner, and the clicks were being detected.

The way to solve it, is to set a custom minimum size for the button; by default, the minimum size is (0,0), but you can set the minumum size to the size you intend your button to be, in order to force it to be that size when added to the HBox. This could get a bit messy depending on how your game uses the buttons and how much they may or may not need to be resized over the course of the game, so depending on how you’re using the buttons, it may be best to handle this minimum size via code. However, in my case the buttons are only ever one size, so I was satisfied with just setting the minimum size via the inspector, which can be done in the Layout section.

tl;dr - when adding buttons to containers at runtime, sometimes the buttons can be resized in unexpected ways, as buttons in godot seem to size themselves in relation to their parent containers. Check your node tree while the game is running (the remote tree, not the local tree) to confirm the size of your buttons.