Skip to reading
Lua Scripting

Give the game a list of clues

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
On this page

LANGUAGE BASICS

A table can hold a list in order. A loop lets you visit each entry without copying the same line over and over.

Code example
local clues = {"west", "bell", "moon"}
for place, clue in ipairs(clues) do
    Log(place .. ". " .. clue)
end

Paste this fragment inside run. The output is 1. west, 2. bell, then 3. moon. Each trip around the loop gets the next place and its word. The two dots join pieces of text.

What changes if you use while?#

Code example
local i = 1
while i <= #clues do
    Log(clues[i])
    i = i + 1
end

Put this below the first example so both can see clues. The # sign counts a list with no gaps. This loop checks its condition before each turn. The last line moves to the next clue. Without that line it would keep printing west.

Pick one clue instead#

Code example
local pick = math.random(1, #clues)
Log("Today's clue is " .. clues[pick])

Ordinary Lua lists start at 1. A Godot Array returned by a PCA helper such as Parallel starts at 0. The language chapter on arrays shows how to tell which kind you have.

Lua scripting   /   Illustrated guide                                                          W08
Page 8 of 133Permalink
Go to page