Skip to reading
Lua Scripting

Arrays, dictionaries and the bridge

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 06

Use ordered slots for a combination and named fields for a clue.

Code example
local Script = {}
Code example
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 }
end
Code example
return Script

Expect 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.

Page 24 of 133Permalink
Go to page