Displaying a text with random fonts

Hello! I'm using the SDK on Windows / VS Code for info and LUA.

I'm trying to load custom fonts and randomly picking one up in order to display it on a sentence.

Here's the line I want to implement the randomly picked custom font :arrow_down_small:
playdate.graphics.drawText(textA[math.random(#textA)], 100, 100)

For now, I'm stuck by just importing to the project a custom font :confused: If anyone can help me, I'll appreciate it :slight_smile:

Here's how I'd do it:

  1. Put all of your fonts in one folder (for example, Source/fonts).
  2. Get a list of all of the files in that folder using playdate.file.listFiles("fonts/"), then store that list as a local variable. In this case, each element is the file name of a font in the folder.
  3. Pick a random element from that list (I found an example here). Save that element to a local variable.
  4. Load the font by doing playdate.graphics.font.new("fonts/" .. random_element), then either pass the resulting loaded font into playdate.graphics.setFont() to set it as the global font, or store it as a variable to be used with the text drawing functions directly.

I'd be happy to provide a code example if this doesn't make sense. I haven't explicitly tested it myself, but unless I'm missing something big, this should work fine.

Hello and thank you for your answer. To be honest I'm not event able to load a custom font by following the Playdate LUA SDK.

Here's what I did so far to just load a custom font:

local fontPaths = {
      [playdate.graphics.font.kVariantNormal] = "fonts/Asterix.fnt",
      [playdate.graphics.font.kVariantBold] = "fonts/MarbleMadness.fnt"
     }

local fontFamily = {
      [playdate.graphics.font.kVariantNormal] = normal_font,
      [playdate.graphics.font.kVariantBold] = bold_font
     }

function playdate.cranked()
      playdate.graphics.getFont(kVariantNormal)
            playdate.graphics.clear()
            playdate.graphics.drawText(textA[math.random(#textA)], 100, 100)
            playdate.graphics.drawText(textB[math.random(#textB)], 100, 120)
            playdate.graphics.drawText(textC[math.random(#textC)], 100, 140)
      end

If you have any clues I'll take them :confused: Have a nice day!

Is there a playdate.graphics.font.newFamily() and playdate.graphics.setFontFamily() call in your code that you haven't shown here? Specifying the paths isn't quite enough, you'll also need to load the fonts off disk and tell the system to use them.

Do you intend to set the bold/italic variants? You don't necessarily have to in order to set the font. My example was written with just the normal variant in mind, but you could adapt it to use font families as described by Dave. Also, do you want each drawText to have a different font/font family? I can provide a code example once you clarify this for me.