mod_gui needs a remove_button function
Posted: Sun Feb 15, 2026 6:18 pm
Sometimes a mod would like to remove a button (or frame) that it previously added. This is trivially done by calling destroy() on the gui element. This will of course remove the element from the button flow or the frame flow.
However, the top frame and inner frame elements still exist. And if the button flow is now empty, this odd artifact remains on the screen. Deleting these frames is possible, and I have implemented some logic to do that (shown below). But since mod_gui frames are a shared space used by multiple mods, it would be better if this logic were contained in mod_gui itself, rather than relying on every mod to reimplement this logic. (The shared nature of mod_gui has already caused issues like Removing a mod can invalidate buttons from other mods in mod_gui flow.)
Here is the code that I recently implemented in a mod:
However, the top frame and inner frame elements still exist. And if the button flow is now empty, this odd artifact remains on the screen. Deleting these frames is possible, and I have implemented some logic to do that (shown below). But since mod_gui frames are a shared space used by multiple mods, it would be better if this logic were contained in mod_gui itself, rather than relying on every mod to reimplement this logic. (The shared nature of mod_gui has already caused issues like Removing a mod can invalidate buttons from other mods in mod_gui flow.)
Here is the code that I recently implemented in a mod:
Code: Select all
-- Clean up mod_gui, if there are no other buttons left
-- Maybe we should check parent.get_mod() first, but
-- ownership of mod_gui is not clear in the first place.
local button_flow = mod_gui.get_button_flow(player)
if #button_flow.children == 0 then
local parent = button_flow.parent
if #parent.children == 1 then
parent.destroy()
end
end