How can i load a font from the host file system (outside res://)?

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

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

:bust_in_silhouette: Reply From: aXu_AP

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:
		dir.list_dir_begin()
		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()
	else:
		print("Font folder not found.")
	return fonts