0 votes

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 extraspacingspace by code? In fact, what is the format in GDscript to change object's property? Thank you.

in Engine by (266 points)

2 Answers

+1 vote
Best answer

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
by (3,247 points)
selected by

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

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

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.

$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.)

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.

0 votes

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


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:

by (597 points)
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 Frequently asked questions and 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 [email protected] with your username.