Read a decision like a sentence
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.
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.")
endThis 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.
| 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
