How to load and set a font at runtime?

I’m currently working on a localisation system and I’d like to have all the necessary files available outside of the actual program and create a possibility to add new translations even after the game has been build.
For that reason I would like the ability to load a font at runtime and set labels and gui text nodes to use this new font.

Is it in any way possible to achieve this?

Defold really likes to know about everything up front to be able to optimize resource use. This obviously doesn’t work well with a strategy of loading and using additional files at runtime. It is not impossible though.

Images can be loaded using either image.load() or through one of the extensions that are available in the asset portal.

Sounds can be loaded using the FMOD extension or through the new sound feature we released where you can load sound data into a buffer and set that on a sound component.

Fonts on the other hand doesn’t support this… A couple of solutions I can think of:

  • Use the resource.font() functionality to define additional fonts and then set those on your labels based on the user language.
  • Add multiple fonts to your gui scenes and use gui.set_font() based on user language.

In both of these cases you need to know beforehand which fonts you are going to need. You can probably figure out quite early on in which markets you wish to launch your games and then make sure to find the required fonts matching the languages in those markets.

You can also use live update to update an installed game with additional content, including fonts.

I already did some digging and feared this would be the answer.
The need to already set up the fonts beforehand is my plan B, which I’m not entierely happy with.

I would have wanted a fully flexible system which would allow to add translations without the need of the creators input or rebuilding the whole game.
This, for example, would allow something like fans being able to do their own translations, even if the game isn’t marketed towards their region.

I haven’t looked into live updating yet, but would it be possible to have a flexible system on which you just load a “language pack” the game didn’t know about before but just does have a loose construct of it in it’s framework?

Is it possible to set resource property for GUI?

It’s possible to add font loading to my text rendering extension GitHub - Lerg/extension-nativetext: Native Text Rendering extension for Defold. and render text to bitmaps with it. It’s much more work but would allow you to do what you wanted.
Currently it uses the default system font, which supports all languages.

1 Like