Font nil after loading?

Windows SDK 1.12.3

I've got a global font, which loads and draws fine. But when I try to call getTextWidth on it I'm getting a nil. So I put some nil checks in the loading code and I'm just flummoxed here.

Here's my loading code:

    print("loading font.")
    font_mpl = font.new("mpl-14.fnt")
    if font_mpl == nil then print("font is nil") end
    gfx.setFont(font_mpl)
    if font_mpl == nil then print("font is nil") end

this prints out "loading font." and then "font is nil" twice, but the font still runs and draws properly.
What is happening here?

Try removing the file extension?

But if it were failing to load the font (it does actually print "Loading: OK") then the call to setFont would fail no? Anyway, I get the same result if I remove the extension. This feels like me using lua wrongly.

Should be playdate.graphics.font.new(path) unless you're creating an alias of it elsewhere in your code.

You could also try playdate.file.exists(path) to confirm your path is correct.

1 Like

Oh oops sorry, in the same file I have

local font <const> = playdate.graphics.font

that's the problem maybe... font this font that

Yeah that's fixed it - I renamed the local to "fnt" and it works now. Phew. Thanks for listening!

1 Like