Arrays, dictionaries and the bridge
LESSON 06
Use ordered slots for a combination and named fields for a clue.
local Script = {}function Script.run(context)
local slots = { "west", "bell", "moon" }
table.insert(slots, "key")
local removed = table.remove(slots, 2)
local counts = {}
for _, word in ipairs(slots) do
counts[word] = (counts[word] or 0) + 1
end
local total = 0
for word, count in pairs(counts) do
total = total + count
end
return { first = slots[1], removed = removed, total = total }
endreturn ScriptExpect west, bell and 3. Ordinary Lua sequences start at 1. ipairs stops at the first missing index.
Keep sequences dense when using #, table.insert or table.remove. pairs visits keys in an
unspecified order. A table assignment shares the same table, so changing a field through one
variable affects both.
PCA converts a table passed to a typed helper to its expected Godot Array or Dictionary. For an
untyped return, a positive Lua sequence length selects an array. Otherwise the bridge selects a
dictionary. Keep a list and its metadata in separate fields. Use Array{} to specify an empty Godot
array. Godot arrays use index 0 for their first entry.
