accesing custom font size via GDscript

gaby424gaby424 Posts: 20Member
edited June 2019 in 2D

Hello,

I'm instantiating in my main scene a button that is in a separate scene.
I can set it`s position easy in my code.

But I can`t do it for the font size. The error I get is : Invalid get index "custom_fonts" (on base: 'button')

Comments

  • TwistedTwiglegTwistedTwigleg Posts: 2,655Admin
    edited June 2019

    Looking at the documentation for the Button node, I am not sure how the editor is setting the font. Apparently according to the theme properties, you can access the font simply through font, but I think you need to get the theme to do that, which would make the code: theme.font.size = 130.

    Looking at the documentation for Control, there are functions called has_font_override and add_font_override that both look promising, but I have no idea how to use either function and the documentation doesn't say.

    Maybe code like this would work? I haven't tested it, but maybe?

    extends Button
    export (Font) var button_font
    # Other code here!
    func on_b_M_pressed():
        # Other code here!
        button_font.size = 130
        user_memory_items[-1][0].add_font_override("font", button_font)
    

    But I'm just guessing at how the add_font_override function works.

    I am also curious to know if there is a way to access the custom font field like in the editor, as I thought this was already exposed to GDScript through a property.

  • DrZanuffDrZanuff Posts: 1Member

    I know this is old, but in case somebody need this in the future, here is my solution:

    $Label.get("custom_fonts/font").size  = font_size # Set Font
    $Label.get("custom_fonts/font").size # Get Font
    
  • HaledireHaledire Posts: 23Member

    Another option is:
    $Label.get_font("font").size

    This is a method inherited from Control. This will get the font currently used by the node whether you have an override or not. It is also possible to look to the inherited theme from this using the type parameter.

Leave a Comment

BoldItalicStrikethroughOrdered listUnordered list
Emoji
Image
Align leftAlign centerAlign rightToggle HTML viewToggle full pageToggle lights
Drop image/file