OK, if this is the root you want to take.
First, string.find
isn’t going to work, because it will reorder the string you’re trying to write. Use a gmatch
iterator matching on .
(means any character) to step through each letter in the string you’re writing:
for letter in myParagraph:gmatch(".") do
then, because doing an if
test on absolutely every character you want to print (26 lower case, 26 uppercase, punctuation, numbers etc) would be incredibly tedious to type, I would store the references to the glyphs in a key: value array. If your keys are just letters you can write them like this
glyphs = {a = "Dropbox:Aglyph", b = "Dropbox:Bglyph}
etc. But if your glyphs start with non-numbers, then you need to use an explicit syntax to indicate that they’re a string,[" "]
:
{["1"] = "Dropbox:1glyph"}
Then, in your loop through the letters, you can just write
for letter in myParagraph:gmatch(".") do
sprite(glyphs[letter])
One last thing. For performance reasons, rather than having 70-odd different textures and spriting each of them in turn, it would be much, much more efficient to put all of your glyphs into a single texture (mono-spaced glyphs would be best here), and have your text drawn as a single mesh
. Using sprite
is fine for throwing together quick mock-ups, but it’s not very perfomant, especially when lots of separate textures are used. This is the sort of thing the texture would look like. You might find a ready made one by googling:

You’d addRect
for each letter, and setRectTex
to point to the correct texture. Eg, say the glyphs are arranged in a square texture, in a 10 x 10 pattern. Lets say “A” is in the top left corner, and they read from left-to-right. The texcoords and width/ height for A would be setRectTex(rect, 0,0.9,0.1,0.1)
. Your glyphs array could store these as vec2s.
glyphs = {a = vec2(0, 0.9), b = vec2(0.1, 0.9), c = vec2(0.2, 0.9)}
Then loop:
for letter in myParagraph:gmatch(".") do
local rect = paragraphMesh:addRect(x,y,w,h)
local g = glyphs[letter]
paragraphMesh:setRectTex(rect, g.x, g.y, 0.1, 0.1)
Or better yet, layout your glyphs in the same order as ASCII 32-122, then use the bytecode for the letter to work out the co-ords, no need for a glyphs array.
for letter in myParagraph:gmatch(".") do
local rect = paragraphMesh:addRect(x,y,w,h)
local ascii = letter:byte() - 32 --ASCII printable characters start at 32
local x = ascii % 10
local y = ascii // 10
local tx = 10/ x
local ty = 0.9 - (10/ y) --invert, texY so that you start from the top of the image
paragraphMesh:setRectTex(rect, tx, ty, 0.1, 0.1)
Right, you got a lot of work to do, so jump to it.