Skip to reading
Lua Scripting

Math, randomness and vectors

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 08

Keep a gauge within its bounds and give a misplaced gear a measurable distance from its socket.

Code example
local Script = {}
Code example
function Script.run(context)
    local charge = math.max(0, math.min(100, 87 + 22))
    local angle = math.rad(90)
    local direction = math.floor(math.sin(angle) + 0.5)
    local choices = { "one chime", "two chimes", "silence" }
    local clue = choices[math.random(1, #choices)]
    local offset = Vector3(3, 0, 4)
    return { charge = charge, north = direction,
             distance = offset:length(), clue = clue }
end
Code example
return Script

Expect charge 100, north 1, distance 5 and one of the three strings. math.floor rounds down and

math.ceil rounds up. math.abs gives magnitude. math.sin and math.cos take radians.

math.random(lo, hi) includes both integers. Use a stored puzzle solution if a random choice must

survive a reload.

The bridge also supplies Clamp, Lerp, Distance, RandomInt and Choose. Use a consistent random

generator when reproducing a bug. math.randomseed controls Lua randomness. PCA's Randomize

controls its engine-side generator.

Page 26 of 133Permalink
Go to page