How do you get a font by name?


  • Posts: 22

I am trying to get a font by name (e.g. "BigRedFont").

The usual way in the engine to do this is to use the
Code: [Select]
getFont(id) function, but that takes the font ID, not its name and that's not very helpful.

I saw this post ( about this topic which says to use the following code:

Code: [Select]
for (res in Data.get().resources)
   if (, Font) && == "Font Name Here")
       return cast (res, Font);       

return null;

However,  this does not work for me. It returns null.
So I went and printed (traced) the whole Data.get().resources object and I noticed that multiple of my fonts are missing from there.

My guess is that the fonts are not loaded yet in the scene, but I was out of luck browsing the Stencyl Haxe API for a solution.

Does anyone know a foolproof method to get a Font by its resource name?

Thanks a lot!


  • *
  • Posts: 2575
Recreate the attached image. It uses a global custom block with a text parameter called "FontName" and an "Import" event. The code reads:
Code: [Select]
import com.stencyl.Data;
Code: [Select]
return cast Data.get().resourceMap.get(__FontName);
My Stencyl resources are available here:
Cutscenes, RPG Elements, Particles, Map System and many more.