List of items (shop, inventory)
In the editor
Section titled “In the editor”- In the
Lojainterface, add a Panel namedItensand, under Arrange children, pick Column. - Inside it, build one item: a Panel
Modelowith a TextNomeand a ButtonComprar. - Uncheck Visible on
Modelo.
In code
Section titled “In code”local itens = { { nome = "Espada", preco = 10 }, { nome = "Escudo", preco = 40 },}
function onSpawn(self) local lista = Ui.get("Loja")["Itens"] for n, item in ipairs(itens) do local linha = lista["Modelo"]:clone("Item" .. n) linha["Nome"]:setText(item.nome) linha["Comprar"]:setText(item.preco .. " moedas") linha["Comprar"]:onClick(self, function() self.moedas = self.moedas - item.preco linha["Comprar"]:setText("Comprado"):setEnabled(false) end) endendstruct Item { std::string nome; int preco;};std::vector<Item> itens = {{"Espada", 10}, {"Escudo", 40}};
void Loja::onSpawn() { Ui::Element lista = Ui::get("Loja")["Itens"]; for (std::size_t n = 0; n < itens.size(); ++n) { Ui::Element linha = lista["Modelo"].clone(std::format("Item{}", n)); linha["Nome"].setText(itens[n].nome); linha["Comprar"].setText(std::format("{} moedas", itens[n].preco)); linha["Comprar"].onClick(this, [this, n, linha] { moedas -= itens[n].preco; linha["Comprar"].setText("Comprado").setEnabled(false); }); }}clone(name)copiesModelowith its contents, puts the copy at the end of the panel and makes it visible.- Calling
clonewith a name that already exists rebuilds the copy: restarting the room does not duplicate the list. linha["Nome"]searches inside that copy.remove()removes a row, and the ones below move up.- List larger than the panel: check Scroll in the panel Inspector.
- To keep purchases between sessions, use Save.
Reference: Interface.
