How to create a Font on runtime to use on a Label?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By henriquelalves
:warning: Old Version Published before Godot 3 was released.

I was trying to change the font of a label on runtime, but for some reason the font wouldn’t be able to load properly.

The code before was:

var f = Font.new()
f.create_from_fnt(*the path of the font*)
label.add_font_override("", f)

But before even reaching the method “add_font_override”, the engine would go all awry with “unicode error: invalid skip” and “unicode error: invalid utf8” on the output. The scene wouldn’t crash, but the Label node I was working with wouldn’t change its Font.

What am I doing wrong? Is there some other way to create a font? Using “ResourceLoader.load(path of the font)” doesnt work too - but instead of just displaying the Label text without anything changed, the text simply disappears.

:bust_in_silhouette: Reply From: henriquelalves

Never mind, I got the error. After a very lucky shot (I was changing the methods almost randomly and forgot to track the arguments), I used “font” as the first argument for “add_font_override”, and now it works properly with a simple “load”.

Is this the intended behaviour of the method? What is the meaning of this first argument?

~EDIT~
Editing only to show the correct code:

label.add_font_override("font",load(*Path of the font*))

The first argument is the “name” of the font used for storing it in a HashMap like this:
data.font_override[p_name]=p_font;

I guess that’s why it can’t/shouldn’t be null.

Hinsbart | 2016-02-23 12:11

But even when I used a random string, like “aaa”, as the first argument, it wasn’t working. It really only works if I use “font”.

I think it’s because I’m allocating the Labels dynamically too, so the default name of its font is “font”, and it is the only way to access it. If it’s because of that, than this is not clear on the documentation, and I’ll try to push a change on it just to help it a little bit.

But I want to be sure of that first, haha.

henriquelalves | 2016-02-23 12:17

It’s because of the way controls and “themes” work. Simplest explanation, the string you put in that first argument isn’t just whatever you want to call it, it’s telling it WHICH font you want to override.

Fonts in controls are “theme items”. What that means is all theme items default to whatever the “theme” sets that item to. Helpful for making broad changes to control objects across your whole project, but when you want to change an individual item of an individual control, you give that control an “override”.

For example, Buttons have several “color” theme items, such as “font_color” and “font_color_pressed”, for when it’s just doing nothing and when it’s being pressed, respectively. So if I wanted to change “font_color_pressed”, I’d use
add_color_override(“font_color”, Color(1,0,0))
or something.

Now, this is a little silly with fonts, since I don’t think there is any control that HAS more than one font property, and therefore you just end up telling it to override the font named “font”. Again simplest explanation, it’s this way because we want fonts to be part of themes.

SolarMongoose | 2016-02-27 16:21

Thank you Henrique so much.

I also found for RichTextLabels, you can do the same method above, but you can add in the font you want that corresponds to the bbcode name, like so:

To enable the [b]tags[/b]:
new.add_font_override("bold_font", load("res://OrbitronBold.fnt"))

And you wonder where I found the bold_font name, hover over the custom font properties in the inspector to get the name.

enter image description here

This is useful for interactive chat boxes (similar to Path of Exile) where you want to use mouse controls for item linkage, colors, bold names, images, etc.

wombatTurkey | 2016-05-24 12:03