Give the game a list of clues
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.
local clues = {"west", "bell", "moon"}
for place, clue in ipairs(clues) do
Log(place .. ". " .. clue)
endPaste 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?#
local i = 1
while i <= #clues do
Log(clues[i])
i = i + 1
endPut 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#
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
