What I propose is that the container GUI elements could accept some parameters that control the visibility or order of their children, executed at display time and not otherwise represented in game state.
Old way, statefully sorts on prototype name:
Code: Select all
-- make parent container
local flow = player.gui.top.add{type = "flow"}
-- get list of item names
local names = {}
local n=0
for name,_ in pairs(pairs(game.item_protytpes)) do
n=n+1
names[n]=name
end
names = table.sort(names)
-- add item buttons to parent in sorted order
for name in names do
flow.add{type="button",caption=game.item_protytpes[name].localised_name}
end
Code: Select all
-- make parent container, tell it how to sort children
local flow = player.gui.top.add{type = "flow", child_sort_attribute = "caption"}
-- add item buttons to parent
for _, prototype in pairs(game.item_protytpes) do
flow.add{type="button",caption=prototype.localised_name}
end
Code: Select all
-- make parent container
local flow = player.gui.top.add{type = "flow"}
-- conditionally add item buttons to parent
for name, prototype in pairs(game.item_protytpes) do
if string.find(name, "basic", 1, true) then
flow.add{type="button",caption=prototype.localised_name}
end
end
Code: Select all
-- make parent container, tell it which children to show
local flow = player.gui.top.add{type = "flow", child_filter_attribute = "caption", child_filter_regex = "basic"}
-- add item buttons to parent
for _, prototype in pairs(game.item_protytpes) do
flow.add{type="button",caption=prototype.localised_name}
end