import{_ as i,a,b as t,ag as h}from"./chunks/framework.BcrMLAmg.js";const g=JSON.parse('{"title":"State Management in Basalt","description":"","frontmatter":{},"headers":[],"relativePath":"guides/states.md","filePath":"guides/states.md","lastUpdated":1744151621000}'),n={name:"guides/states.md"};function l(k,s,e,p,E,d){return t(),a("div",null,s[0]||(s[0]=[h(`
States provide a new way to manage data and UI synchronization in your Basalt applications.
BaseFrame:initializeState("name", defaultValue, persistent, path?)Creates a new state in a BaseFrame:
name: Name of the state (string)defaultValue: Initial value of the state (any type)persistent?: Boolean that determines if the state should be stored into a filepath?: The path the state should be stored to (default: states/BaseFrameNAME.state)element:setState("name", newValue)Updates an existing state's value:
name: Name of the state to updatenewValue: New value to setlocal value = element:getState("name")Retrieves the current value of a state:
name: Name of the state to getelement:computed("name", function(self)
local otherState = self:getState("otherState")
return someCalculation(otherState)
end)Creates a computed state that depends on other states:
name: Name of the computed statefunction: Function that calculates the valueelement:onStateChange("name", function(self, newValue)
-- React to changes
self:someAction(newValue)
end)Registers a listener for state changes:
name: Name of the state to watchfunction: Callback function receiving the new valuelocal name = form:addInput():bind("text", "name")Binds a property to a state
propertyName: The name of the propertystateName: The name of the stateHere's a comprehensive example showing state management in a form:
local main = basalt.getMainFrame()
-- Initialize form states
:initializeState("username", "", true) -- make them persistent
:initializeState("password", "", true) -- make them persistent
:initializeState("confirmPassword", "", true) -- make them persistent
local form = main:addFrame()
:setSize("{parent.width - 4}", "{parent.height - 4}")
:setPosition(3, 3)
-- Add computed validation state
form:computed("isValid", function(self)
local username = self:getState("username")
local password = self:getState("password")
local confirmPass = self:getState("confirmPassword")
return #username >= 3 and #password >= 6 and password == confirmPass
end)
-- Create labels
form:addLabel({text="Username:", x = 2, y = 2, foreground = colors.lightGray})
form:addLabel({text="Password:", x = 2, y = 4, foreground = colors.lightGray})
form:addLabel({text="Confirm:", x = 2, y = 6, foreground = colors.lightGray})
local userInput = form:addInput({x = 11, y = 2, width = 20, height = 1}):bind("text", "username")
local passwordInput = form:addInput({x = 11, y = 4, width = 20, height = 1}):bind("text", "password")
local confirmInput = form:addInput({x = 11, y = 6, width = 20, height = 1}):bind("text", "confirmPassword")
-- Submit button
local submitBtn = form:addButton()
:setText("Submit")
:setPosition(2, 8)
:setSize(29, 1)
-- Status label
local statusLabel = form:addLabel()
:setPosition(2, 10)
:setSize(29, 1)
form:onStateChange("isValid", function(self, isValid)
if isValid then
statusLabel:setText("Form is valid!")
:setForeground(colors.green)
submitBtn:setBackground(colors.green)
else
statusLabel:setText("Please fill all fields correctly")
:setForeground(colors.red)
submitBtn:setBackground(colors.red)
end
end)State Initialization
Computed States
State Updates
Common Patterns