Skip to reading
Lua Scripting

Functions and reusable text

Edition 0.86.4.1 HF1

0.86.4.1 Hotfix 1 edition. Earlier lessons and screenshots retain their original version context; revised references and supplements describe the newer behavior.

Download PDFView PDF page

LESSON 07

Normalize an answer once so a player's spacing and capitalization do not change the puzzle.

Code example
local Script = {}
Code example
local function tidy(text)
    return (text:match("^%s*(.-)%s*$"))
end
Code example
function Script.normalize(text)
    return string.lower(tidy(tostring(text or "")))
end
Code example
function Script.run(context)
    local answer = Script.normalize("  WeSt  ")
    local label = string.format("Dial %02d / %s", 3, answer)
    return label
end
Code example
return Script

Expected result: Dial 03 / west. The local function remains private. normalize is on the returned

table, so another PCA record can call it by name. The ^ and $ pattern anchors mark the ends, %s

matches whitespace, and the capture (.-) takes the shortest middle that fits.

Use text:sub(1, 4) for a slice, text:find(".", 1, true) for a literal dot, and text:gsub("bell", "chime") for

replacement. gsub returns the new string and a replacement count. Strings are immutable. Lua

string indexes count bytes. Use utf8.len for the number of UTF-8 code points and PCA's Lower or

Upper for engine-side text handling.

Page 25 of 133Permalink
Go to page