#Set Button Size

1 messages · Page 1 of 1 (latest)

fleet timber
#

How do I set the size of a TextureButton.
I am currently trying this
button.rect_min_size = Vector2(card_w, card_h)
and I am getting Error:
invalid set index rect_min_size on base texture button with value of type vector2

#

maybe you need this also

    var button_index = index
    var button = grid.get_child(button_index)

    # Check if the card is already revealed or matches an already revealed card
    if revealed_cards.size() < 2 and !revealed_cards.has(button_index):
        revealed_cards.append(button_index)
        reveal_card(button_index)
        
        if revealed_cards.size() == 2:
            set_process_input(false)  # Disable further input until checking for match
            call_deferred("_check_for_match")  # Call after a brief delay```
brisk rivet
#

I’m not quite sure what you’re attempting to do, but if you’re setting the size of the TextureButton, you should be setting the size of the textures themselves, as that is what Godot actually samples for UI interactions. I’ve never used this property to set my button size before.

#

In other words, the clickable/focusable/pressable area of a TextureButton is determined by the size of your texture.

chilly dirge
#

You're using Godot 3.x syntax. The property was renamed to custom_minimum_size in 4.x, it isn't rect_min_size anymore.

For future reference, any help/tutorial posts you find online from before 2023 or so are written for 3.x, and will most likely need to be updated for 4.x.

chilly dirge
brisk rivet
#

Oh, they were trying to do minimum size. That makes sense, as I didn’t recognize the property name.

fleet timber
#

wow guys thanks so much for the answers. so pleased with this community so far!

#
extends Control

var card_w
var card_h
var num_columns = 6
var num_rows = 3
var num_pairs = num_columns * num_rows / 2
var images = {
    0: "res://deck_retro_king.jpg",
    1: "res://deck_retro_king.jpg",
    2: "res://deck_retro_king.jpg",
    3: "res://deck_retro_king.jpg"
}
var shuffled_images = []
var revealed_cards = []
var grid

func _ready():
    var screen_size = get_viewport_rect().size
    card_w = screen_size.x / 11
    card_h = screen_size.y / 5

    grid = $GridContainer
    grid.columns = num_columns
    setup_game()
    setup_cards()

func setup_game():
    var image_keys = images.keys()
    image_keys.shuffle()
    for i in range(num_pairs):
        shuffled_images.append(images[0]) #image_keys[i % image_keys.size()]  make random image select 
    print(shuffled_images)
    shuffled_images += shuffled_images.duplicate()
    shuffled_images.shuffle()
    print(shuffled_images)

func setup_cards():
    for i in range(num_columns * num_rows):
        var card = create_card(i)
        grid.add_child(card)

func create_card(index):
    var button := TextureButton.new()
    button.custom_minimum_size = Vector2(card_w, card_h)
    button.name = str(index)
    button.pressed.connect(self._on_Card_pressed.bind(index))  # Connect the pressed signal to _on_Card_pressed
    return button

func _on_Card_pressed(index):
    var button_index = index
    var button = grid.get_child(button_index)

    # Check if the card is already revealed or matches an already revealed card
    if revealed_cards.size() < 2 and !revealed_cards.has(button_index):
        revealed_cards.append(button_index)
        reveal_card(button_index)
        
        if revealed_cards.size() == 2:
            set_process_input(false)  # Disable further input until checking for match
            call_deferred("_check_for_match")  # Call after a brief delay
#
func reveal_card(index):
    var button = grid.get_child(index)
    var image_texture = load(shuffled_images[index])
    button.texture_normal = image_texture
    button.texture_pressed = image_texture
    button.texture_hover = image_texture

func hide_card(index):
    var button = grid.get_child(index)
    button.texture_normal = null
    button.texture_pressed = null
    button.texture_hover = null

func _check_for_match():
    if shuffled_images[revealed_cards[0]] == shuffled_images[revealed_cards[1]]:
        revealed_cards.clear()  # Clear revealed cards on match
    else:
        hide_card(revealed_cards[0])  # Hide non-matching cards
        hide_card(revealed_cards[1])
    revealed_cards.clear()  # Clear regardless of match status
    set_process_input(true)  # Re-enable input after checking

#

this is where i am right now.
I havent done the texture resize yet, ill look at doing that now.
But in the mean time i have an issue where the buttons do not show at all.
I currently run with no errors

fleet timber
#

im playing around with setting image size and cannot figure it out.
I have tried a few things including set_size_override.
i believe my issue lies in the proper way to initialize and load and resize and image. i assume im doing an incorrect order of these tasks

chilly dirge
#

You can't resize the texture itself in Godot. You'll need to do that externally. You can resize nodes that display the texture, but not the texture resource.