If you want to create the dynamic font from the editor, you can press the New Resource button in the inspector dock, and select DynamicFont. Set its properties and then use the floppy button to save it.
You can create an auto-load script with this code:
extends Node
var fonts = []
func create_dynamic_font(font_name, font_size, outline_size = 3, outline_color = Color(0, 0, 0)):
var id = "%s-%s-%s-%s" % [font_name, font_size, outline_size, outline_color]
var r = check_for_id_added(id)
if r != -1: return r
if (font_size < 1): font_size = 1
var font = DynamicFont.new()
var fontData = DynamicFontData.new()
font.size = font_size
if font_name.get_extension() != "ttf":
fontData.font_path = "res://Fonts/%s.ttf" % font_name
else:
fontData.font_path = "res://Fonts/%s." % font_name
font.outline_size = outline_size
font.outline_color = outline_color
font.font_data = fontData
var new_font = { "id" : id, "src" : font}
fonts.append(new_font)
return fonts.size() - 1
func get_font(font_name, font_size, outline_size=0, outline_color=Color(0,0,0)):
var index = create_dynamic_font(font_name, font_size, outline_size, outline_color)
return fonts[index].src
func check_for_id_added(id):
for i in range(fonts.size()):
var struct = fonts[i]
if struct.id == id: return i
return -1
then you can use this command in any gdscript:
var myfont = Your_autoload_script.get_font(font_name, font_size, outline_size, outline_color)
You need puts fonts in a folder called “Fonts” in your project (create it if does not exists). You can only use fonts with extension “.ttf”
Example:
# Create a font "Arial" with size 26, outline size 3 and outline color red.
# File "Arial.ttf" is in folder "Fonts" in your project
var myfont = Your_autoload_script.get_font("Arial", 26, 3, Color(1,0,0))