Skip to content

Work with app state

Every App has one store, app.state: a tree of plain tables that holds the UI’s state and, if you like, the game data the UI shows. Anything that reads a key runs again when that key changes, and nothing else does. This guide builds a small track queue for the Booth to show how to read, write and bind to it.

Start with your data

Pass the starting tree to App.new. Components add their own state under ui when they are created, so keep your data beside it:

local app = Lucy.App.new({
    theme = "dark",
    state = {
        player = { coins = 0 },
        booth = { queue = {} },
        settings = { lights = true },
    },
})

Paths are dot separated: "player.coins". A part made only of digits is an array index, so "booth.queue.1.title" is the first track’s title.

Read and write

app.state:set("player.coins", 10)
app.state:update("player.coins", function(coins)
    return coins + 5
end)
app.state:merge("player", { coins = 0, level = 3 }) -- sets those keys, keeps the rest
app.state:remove("player.level")

local player = app.state:get("player")
print(player.coins)

set stores a copy of what you pass and compares it with what is there, key by key. Writing an equal value notifies nobody, so it is fine to set a whole table each time something might have changed.

get returns a frozen copy: it never changes under you, and assigning to it throws an error. Lucy reuses the copy until something inside changes. Use get for one off reads; to follow a value, use a ref.

Follow a path with state:ref

app.state:ref(path) is a ref onto one path. Read it inside a binding and the binding follows that path:

local coins = app.state:ref("player.coins")

Lucy.new "TextLabel" {
    Text = function()
        return `Coins: {coins()}`
    end,
    Parent = scene.gui,
}

coins(coins() + 10) -- the same as app.state:set("player.coins", ...)

It sees writes made from anywhere: game code calling set, another ref onto the same path, or a component bound to it. A path that does not exist yet reads as nil, and the ref picks it up when it appears.

Bind a component prop

Pass a ref as a component prop and the component’s key follows it both ways. A Switch bound to settings.lights writes the setting when clicked and moves when the setting changes:

Work with app state exampleWork with app state example
Lucy.Switch.new({
    label = "Club lights",
    checked = app.state:ref("settings.lights"),
    Parent = scene.gui,
})

There is only one copy of the value, so nothing needs to be kept in sync.

Use the tree like tables with state.root

app.state.root is the store as tables you can index, loop over and assign to. Reading a key follows that key. Assigning goes through set, so bindings and subscribers see it:

local root = app.state.root

root.player.coins += 1
for i, track in root.booth.queue do
    print(i, track.title)
end
print(#root.booth.queue)

A loop or # follows the whole table, so it runs again when anything in it changes. Reading one key follows only that key.

Show a list

Lucy.each makes one row per item and keeps rows by key, so when the list changes, rows for items that stay are kept and moved, not rebuilt:

local queue = app.state:ref("booth.queue")
local root = app.state.root

Lucy.new "Frame" {
    Parent = scene.gui,
    Lucy.Component.list({ Padding = UDim.new(0, 4) }),
    Lucy.each(queue, function(track, index)
        return Lucy.new "TextLabel" {
            Name = track.id,
            Text = function()
                local entry = root.booth.queue[index()]
                return if entry then `{entry.title} ({entry.votes} votes)` else ""
            end,
        }
    end, { key = function(track)
        return track.id
    end }),
}

track is the item as it was when its row was made: use it for things that never change, such as the id. For fields that do change, read the store through index(), which follows the row as it moves. Then a write such as root.booth.queue[2].votes += 1 updates that one label.

To add or remove tracks, set the list:

local tracks = table.clone(app.state:get("booth.queue"))
table.insert(tracks, { id = "sunset-groove", title = "Sunset Groove", votes = 1 })
app.state:set("booth.queue", tracks)

Control components from game code

A component’s state lives at ui.<id>. Give it an id and any code can drive it without holding the handle:

Work with app state exampleWork with app state example
Lucy.Button.new({ id = "buyMoonwalk", text = "Buy Moonwalk", Parent = scene.gui })

-- elsewhere
app.state:set("ui.buyMoonwalk.disabled", app.state:get("player.coins") < 300)
app.state:merge("ui.buyMoonwalk", { text = "Owned", disabled = true })

Run code when a value changes

Bindings keep the UI in step. For work outside the UI, such as saving, playing a sound or telling the server, subscribe:

local stop = app.state:subscribe("player.coins", function(new, old)
    if new > old then
        coinSound:Play()
    end
end)

A subscriber on a table hears about changes anywhere under it, with old and new frozen copies. Subscribers run after bindings and effects. Call the returned function to stop.

Write several values at once

Wrap related writes in app.state:batch (the same as Lucy.batch). Bindings and subscribers run once at the end, with the final values:

app.state:batch(function()
    app.state:set("player.coins", 0)
    app.state:set("booth.queue", {})
    app.state:set("ui.buyMoonwalk.disabled", true)
end)

A subscriber whose value ends where it started is not called at all.

Next