Skip to reading
Lua Scripting

Operators and decisions

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 04

Use arithmetic to drive a puzzle, then choose exactly one response.

Code example
local Script = {}
Code example
function Script.run(context)
    local ticks = 17
    local turns = ticks // 6
    local remainder = ticks % 6
    local response
    if turns < 1 then
        response = "Wind the clock."
    elseif remainder == 0 and turns >= 2 then
        response = "The teeth line up."
    else
        response = "Advance " .. (6 - remainder) .. " tick(s)."
    end
    return response
end
Code example
return Script

The result is “Advance 1 tick(s).” Change ticks to 12 for the aligned branch. Integer floor division //

counts completed turns. The % operator returns the remainder. The / operator returns a

floating-point quotient. Use +, -, *, and ^ for addition, subtraction, multiplication and powers.

Use = to assign, == or ~= to test equality, and <, <=, > or >= to compare. Parentheses make

precedence explicit. The .. operator joins strings. For branching, and and or return operands, and

not returns a Boolean. Distinguish a missing setting from false with an explicit value == nil check.

Page 22 of 133Permalink
Go to page