How do you get a font by name?

daverave1212

  • Posts: 44
Hello!

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 (http://community.stencyl.com/index.php?topic=45647.0) about this topic which says to use the following code:

Code: [Select]
for (res in Data.get().resources)
{
   if (Std.is(res, Font) && res.name == "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!

Luyren

  • *
  • Posts: 2807
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: https://luyren.itch.io/
Cutscenes, RPG Elements, Particles, Map System and many more.