What's the best way to create a 2D grid of ColorRects?

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

Hello! My objective is to create a responsive container of any size and in it a 2D grid of ColorRects that fills the full size of the container.

GridContainer shrinks the ColorRects to size 0 if Min Size is 0, so I tried to set the Min Size as proportional to the size of the container using GridContainer.get_rect().size. The problem is that with some configurations (for example setting Layout Full Rect) of the GridContainer this size returns 0, even if the real size of the container is not 0. This was an unexpected behaviour for me.

How should I go about creating my grid? Maybe GridContainer is not the way, maybe I need additional containers or maybe there is another function that returns the size of the container.

Is this going to be fundament of your project ?
Did You consider using TileMap ?
Why do You need to change size of this grid in-game ?

Inces | 2021-10-14 15:39

I consider using TileMap but hasn’t tried because it’s not what I need, I don’t need predefined tiles, I need rects that I can change their color from scripts.

The size change may be needed for making the grid responsive to different window sizes.

cris177 | 2021-10-14 15:57

But TileMap is most responsive node script-wise. It is has many methods that make elastic uses of it, while Control nodes are much more stiff. It is easy to make a system detecting your clicks and changing color of chosen tile. You can overlay more than one TIlemaps to create additional functionality. Window size is not a problem, You will adapt it in Viewport settings. Tilemap would only have problem with rects of different sizes, or with changing real global size midgame.

Maybe You can tell more about Your project ? What will those rects do, how do they interact with each other ?

Inces | 2021-10-14 17:27

Basically there are a couple of variable values that determine the color of each tile in a 10x10 grid of tiles. The color of each tile is determined once the grid is created, so the only needed functionality is determine the color based on its x, y coordinate on the grid (and the other values).

For example the colors of the tiles could be Color(ax/9, ba, a*y/9) where x, y is the grid position of the tile and a, b other game variables. All that needs to be done is generate the tiles of each corresponding color.

I’ll look into how to implement this with tilemaps if you insist, but it didn’t seem like the right choice to me.

cris177 | 2021-10-14 17:49

This might sound obvious but did you try to set your ColorRects to expand instead of setting a minimum size?

skysphr | 2021-10-16 01:21

Yes, doesn’t work, the grid still sets their size as their min size.

cris177 | 2021-10-16 01:49