Godot Version
Godot 4
Question
Hello! I’m very very new to Godot, and have come across an issue.
I’m trying to dynamically populate a GridContainer with some TextureButtons; to be precise, I create Texture Buttons based on the textures I have in a resource folder. Now, I’d like to access these TextureButtons for the sake of customization, however my program can’t seem to find any children to my grid Container.
Here’s my grid container code:
func _ready():
_update_grid()
func _update_grid():
var button_size = Vector2(50, 50)
var h_separation = get_theme_constant("h_separation")
#this functions calculates some margin space, unrelevant
var available_space = get_available_space()
#build grid
columns = 4
var resize = available_space.x / columns
button_size = Vector2(resize - h_separation, resize - h_separation)
columns = floori(available_space.x / button_size.x)
#function to update texture buttons; for now only the size is changed
for child in self.get_children():
if child is TextureButton:
child.ignore_texture_size = true
child.custom_minimum_size = button_size
The population itself is made in a parent node, a margin container, and any code written here does work since I’m working on the Texture Buttons as soon as they’re created. The function is written like so (this was taken from a tutorial on youtube, I only added a couple lines to ignore the original texture size):
func populate_grid(cards):
for card in cards:
#removes .imported and goes to the original name
var adjusted_path = card.substr(0, card.length() - 7)
var texture_button = TextureButton.new()
texture_button.set_meta("file_path", adjusted_path) #to use in the save file
texture_button.stretch_mode = TextureButton.STRETCH_KEEP_ASPECT_COVERED
texture_button.mouse_filter = Control.MOUSE_FILTER_PASS
texture_button.texture_normal = ResourceLoader.load(adjusted_path)
texture_button.ignore_texture_size = true
texture_button.stretch_mode = TextureButton.STRETCH_KEEP_ASPECT
texture_button.custom_minimum_size = Vector2(50, 50)
texture_button.connect("pressed", Callable(self, "_on_bg_pressed").bind(texture_button))
# cardsGrid references the grid container
cardsGrid.add_child(texture_button)
How do I access the TextureButtons from inside the Grid Container? It’s telling me there’s no children, but it’s clearly not true since the texture buttons are there when I run the program.
Anything I’m doing wrong?