how to change property of dynamic-font by code

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

This might sound very basic, I could change the spacing of words of a label with the editor. However, when the text is updated by code, the property setting does work. How do I change the extra_spacing_space by code? In fact, what is the format in GDscript to change object’s property? Thank you.

:bust_in_silhouette: Reply From: Magso

The custom font hasn’t got it’s own ‘get font’ method so the code will need the generic get() function.

$label.get("custom_fonts/font").extra_spacing_char = number

Thank you very much, it works nicely now. I made a mistake by putting the extra_spacing_charinside the bracket as I thought it is also a property. Just a follow up question, both custom_fonts/font and extra_spacing_char are property is that any rules why one is in the bracket and the other is not? Thank you.

Idleman | 2020-07-04 23:45

To understand it, custom_fonts/font is a property of the label which will get the dynamic font resource. extra_spacing_char is a property of the dynamic font resource.

In order to access the dynamic font from the label it needs to be returned via a method which is what the brackets are so get(), translate(), get_children(), etc are all methods that belong to specific node types.

The properties are the values that can be accessed from that node or resource so get("custom_fonts/font") returns the resource and now it has access to the extra_spacing_char property.

Also properties can have set and get like Node2D.get_position() which is the same as Node2D.position

Magso | 2020-07-05 00:48

Thank you for explain it in detail, I was having some difficulties to understand how set and get works, now I have a better understanding.

Idleman | 2020-07-05 01:12

$Label.get_font("font") is a simpler way to do it than $Label.get("custom_fonts/font").

(In Godot 4.0, it will be $Label.get_theme_font("font") instead.)

Calinou | 2020-07-05 12:02

In the Label docs I saw font under Theme Properties but it wasn’t clear if it was using the theme property or the custom font.

Magso | 2020-07-05 12:58

:bust_in_silhouette: Reply From: scrubswithnosleeves

Just to clear this up, I think the most optimal way is:

$Label.get_font("font").*property*

where property is what pops up when you hover your mouse over the property you want to change in the inspector viewport. (“font”) should literally be “font”, not the name of the font or anything haha.

so in this case:

$Label.get_font("font").extra_spacing_char