Property is a utility module in RoClass that allows you to define and manage class properties with custom behaviors.
It provides getter/setter functionality, default values, and property definitions for objects.
The Property module provides tools to:
--!strict
Property.add(target: table, name: string, definition: table | any): nil
Adds a property to a class or instance.
target
→ Class or instance table.name
→ Name of the property.definition
→ Either a value or a table containing:
default
→ Default valueget
→ Optional getter function (self) -> any
set
→ Optional setter function (self, value) -> ()
local Player = RoClass.new("Player")
:constructor(function(self)
self.Health = 100
end)
:build()
-- Add a property to an instance
RoClass.AddProp(Player, "Mana", 50)
local p1 = Player.new()
print(p1.Mana) -- 50
RoClass.AddProp(Player, "Health", {
default = 100,
get = function(self)
return self._health
end,
set = function(self, value)
self._health = math.clamp(value, 0, 100)
end
})
local p1 = Player.new()
p1.Health = 150
print(p1.Health) -- 100, clamped
This documentation is part of RoClass. For more examples and detailed guides, see RoClass Docs.