Skip to reading
Lua Scripting

Read what the player typed

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
On this page

TEXT AND INPUT

A player may enter “west”, “WEST” or a word surrounded by spaces. Decide which differences matter before comparing their answer.

Read the live text#

Code example
local raw = GetGUIElementProperty(
    "archive_panel", "answer_entry", "text", "")

The first two arguments identify the GUI and its text box. The third requests its current text. The empty string at the end is a fallback if the lookup fails. Substitute the IDs used by your own GUI.

Clean it before comparing#

Code example
local text = tostring(raw)
local trimmed = text:match("^%s*(.-)%s*$")
local answer = string.lower(trimmed)

The pattern removes spaces from the two ends. It keeps spaces inside the answer. Lowercasing then turns WeSt into west. The comparison now accepts both.

Give a helpful reply#

Code example
local reply = "Look for wear on one of the dials."
if answer == "" then
    reply = "Type a direction first."
elseif answer == "west" then
    reply = "The lock releases."
end
SetGUIElementProperty("archive_panel", "feedback", "text", reply)

These three fragments belong inside the same function. The final line puts the chosen reply in a label. An empty answer gets its own message so the player knows what to do next.

Lua scripting   /   Illustrated guide                                                          W11
Page 11 of 133Permalink
Go to page