Skip to reading
Lua Scripting

Loops that finish

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 05

Build a list of clock chimes and find the first one that opens the latch.

Code example
local Script = {}
Code example
function Script.run(context)
    local sounds = {}
    for bell = 1, 5, 2 do
        sounds[#sounds + 1] = bell
    end
    local i = 1
    while i <= #sounds do
        if sounds[i] == 3 then break end
        i = i + 1
    end
    local winding = 0
    repeat
        winding = winding + 1
    until winding >= 2
    return { position = i, turns = winding, bells = sounds }
end
Code example
return Script

The numeric for includes its endpoint when the step lands there, producing 1, 3, 5. The while checks

before entering and stops at index 2. The repeat loop checks after the body, so it always runs at

least once.

Use break to leave the nearest loop. To skip an item, put the work inside if eligible then … end.

Choose a finite bound when scanning inventory. A tight loop waiting for a click prevents the runtime

from processing that click. Use a GUI action or a signal for input.

Page 23 of 133Permalink
Go to page