Skip to reading
Lua Scripting

Read a decision like a sentence

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

LANGUAGE BASICS

The player types 02 into a box. Is that two turns of the dial, or just two characters? Lua can work with either meaning.

Code example
local limit = 3
local typed = "02"
local turns = tonumber(typed)
if turns == nil then
    Log("Type a number.")
elseif turns > limit then
    Log("Too many turns.")
else
    Log("The dial moves.")
end

This is a fragment. To try it, put it inside function Script.run(context), before its end. Keep return Script at the bottom of the record.

Source table from Read a decision like a sentence, page 7
typed = "02"tonumber produces 2. The number fits within the limit, so you see “The dial moves.”
typed = "8"Lua skips the first branch and takes the elseif branch. You see “Too many turns.”
typed = "bell"tonumber produces nil, meaning there is no number here. The first branch handles it before Lua compares it to 3.

Three little marks worth learning#

= gives a name a value. == asks whether two values match. -- starts a comment, so you can leave yourself a note. For a limit you intend to keep fixed in Lua 5.4, write local LIMIT <const> = 3.

Lua scripting   /   Illustrated guide                                                          W07
Page 7 of 133Permalink
Go to page