Skip to content

Persistent state

Goal: remember whether the HUD was hidden when the module was disabled normally.

if Storage.Layout == nil then
Storage.Layout = {
version = 1,
visible = true
}
end
local Visible = Storage.Layout.visible
local ToggleAction = InputActions:FindAction("TogglePanel")
if ToggleAction ~= nil then
ToggleAction:OnPerformed(function()
Visible = not Visible
Storage.Layout.visible = Visible
end)
end
Scheduler:OnFrame(function()
if Visible then
ImGui:DrawDebugText("Persistent panel")
end
end)

false is not missing. Initialize the table only when Storage.Layout == nil.

Perform a small migration when the format changes:

if Storage.Layout == nil or Storage.Layout.version ~= 2 then
local oldVisible = true
if Storage.Layout ~= nil
and Storage.Layout.visible ~= nil then
oldVisible = Storage.Layout.visible
end
Storage.Layout = {
version = 2,
visible = oldVisible,
compact = false
}
end
  • strings, numbers, booleans, and nested tables persist;
  • split userdata such as Vector3 into numbers;
  • assigning nil removes a key after save;
  • hot reload does not serialize current state;
  • test by disabling and enabling the module normally.

Reference: Storage, InputAction, and Vector3 for serializing structured values.