0 votes

This link (https://docs.godotengine.org/en/stable/classes/class_dynamicfont.html) has an example, but I can't use 'res://' in my project.

in Engine by (23 points)

1 Answer

0 votes
Best answer

You can use Directory class to access outside folders too. In this example, I use Windows enviromental variable to access Fonts folder (usually "C:\Windows\Fonts"), get all files ending with ttf or otf and load one of the fonts (have a label node named Label):

func _ready():
    # Get list of font files
    var fonts = get_fonts()
    # Get one of the fonts
    var selected_font = fonts[0]
    var dynamic_font = DynamicFont.new()
    dynamic_font.font_data = load(selected_font)
    dynamic_font.size = 64
    $Label.set("custom_fonts/font", dynamic_font)

func get_fonts() -> Array:
    # This works only on Windows
    var fonts_dir = OS.get_environment("windir") + "\\Fonts"
    var dir = Directory.new()
    var fonts = []
    if dir.open(fonts_dir) == OK:
        var file_name = dir.get_next()
        while file_name != "":
            if not dir.current_is_dir() and (file_name.ends_with("ttf") or file_name.ends_with("otf")):
                fonts.append(fonts_dir + "\\" + file_name)
            file_name = dir.get_next()
        print("Font folder not found.")
    return fonts
by (382 points)
selected by
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.