I'm trying to start using AssetManager in my game. Previously I used to generate two BitmapFonts (bigger and smaller) from my .ttf file and it worked well. Here's how it looked in the code:

FreeTypeFontGenerator generatorRoboto = new FreeTypeFontGenerator(Gdx.files.internal("fonts/Roboto-Regular.ttf"));
FreeTypeFontGenerator.FreeTypeFontParameter params = new FreeTypeFontGenerator.FreeTypeFontParameter();
params.size = 80;
BitmapFont robotoFont = generatorRoboto.generateFont(params);
robotoFont.getRegion().getTexture().setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear);
params.size = 40;
BitmapFont smallerFont = generatorRoboto.generateFont(params);

Then I add it to skin and use it.

skin.add("roboto-regular-font", robotoFont, BitmapFont.class);
skin.add("roboto-smaller-font", smallerFont, BitmapFont.class);

It works well, but now I'm trying to use it with AssetManager (according to documentation):

FreeTypeFontLoaderParameter robotoFontLP = new FreeTypeFontLoaderParameter();
robotoFontLP.fontFileName = "fonts/Roboto-Regular.ttf";
robotoFontLP.fontParameters.size = 80;
manager.load("fonts/Roboto-Regular.ttf", BitmapFont.class, robotoFontLP);

BitmapFont robotoFontBig = manager.get("fonts/Roboto-Regular.ttf", BitmapFont.class);
robotoFontBig.getRegion().getTexture().setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear);
BitmapFont robotoFontSmall = manager.get("fonts/Roboto-Regular.ttf", BitmapFont.class);
robotoFontSmall.getRegion().getTexture().setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear);

skin.add("roboto-regular-font", robotoFontBig, BitmapFont.class);
skin.add("roboto-smaller-font", robotoFontSmall, BitmapFont.class);

But it doesn't work, LibGDX treat them as the same font. What am I doing wrong?


1 Answer 1


I believe this could be an issue with the AssetManager as it uses an objectMap which maps loaded objects to the string used to load them. In this case you're loading the font and the assetManager is storing it in it's objectMap under the key "fonts/Roboto-Regular.ttf", then when you load your font again with second set of parameters the previous font is overwritten.

I found this workaround from this page.

One solution would be to pass an id as the name, and also have parameters that get passed to the TTF loader that actually contains the filename.

This example shows how to use the parameter file to define font file name leaving the name in the load method to be used purely as a key. Adapted from the libgdx ttf test found here

    FreeTypeFontLoaderParameter robotoFontBigParam = new FreeTypeFontLoaderParameter();
    size1Params.fontFileName = "fonts/Roboto-Regular.ttf";
    size1Params.fontParameters.size = 40;
    manager.load("size40.ttf", BitmapFont.class, robotoFontBigParam );

    FreeTypeFontLoaderParameter robotoFontSmallParam= new FreeTypeFontLoaderParameter();
    size2Params.fontFileName = "fonts/Roboto-Regular.ttf";
    size2Params.fontParameters.size = 12;
    manager.load("size12.ttf", BitmapFont.class, robotoFontSmallParam);
  • \$\begingroup\$ I have no idea how to do that, could you please explain in code? \$\endgroup\$
    – Divelix
    Commented Sep 14, 2017 at 9:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .