import{_ as i,a,b as e,ag as t}from"./chunks/framework.BcrMLAmg.js";const E=JSON.parse('{"title":"Properties in Basalt","description":"","frontmatter":{},"headers":[],"relativePath":"guides/properties.md","filePath":"guides/properties.md","lastUpdated":1740475938000}'),n={name:"guides/properties.md"};function l(h,s,p,k,r,d){return e(),a("div",null,s[0]||(s[0]=[t(`
Properties are a core concept in Basalt that control how elements store and access their values.
You can set properties when creating elements:
-- Initialize multiple properties at creation
local button = main:addButton({
x = 5,
y = 5,
width = 10,
height = 3,
text = "Click me"
})
-- Equivalent to:
local button = main:addButton()
:setX(5)
:setY(5)
:setSize(10, 3)
:setText("Click me")There are three ways to interact with properties, each with different characteristics:
element:setX(5) -- Most validation, observers called
local x = element:getX()element.x = 5 -- Convenient, observers called
local x = element.xelement.set("x", 5) -- Minimal validation, observers called
local x = element.get("x")These are special methods that modify multiple properties at once:
-- These modify x and y properties together
element:setPosition(5, 5)
-- These modify width and height properties together
element:setSize(10, 3)Any property can be set to a function that gets called on access:
-- Function as property value
element.x = function(self)
return self:getParent():getWidth() - 10
end
-- Function gets called when accessing x
local xPos = element.x -- Calls the functionWith the reactive plugin, you can use string expressions that automatically update:
-- Requires reactive plugin
element:setX("{parent.width - 10}") -- Updates when parent width changesYou can react to property changes:
element:onChange("x", function(self, newX)
-- Called when x changes through any method
end)Choose the Right Access Method
:setX() when you need validation.x for convenient access.set() for maximum performanceDynamic Properties
Combined Properties
setPosition modifies x and ysetSize modifies width and height