props
Github Repository
Roblox Asset
Props is a lightweight module written in strict Luau designed to offer a streamlined and flexible interface for managing values with getter and setter functions, inspired by C# accessor syntax and Redux.
This module allows you to create a prop
object with a type T
, based on the initial value given. The prop
object provides a simple way to manage and track the value changes using middleware and signals. The value of the property is protected as it is hidden out of scope.
What it Offers
-
Getter and Setter Functions: The prop object provides two primary methods:
-
get
: Retrieves the current value, with middleware applied. -
set
: Updates the value, applying middleware and firing the change signal if the value differs.
-
-
Middleware Support: Middleware functions can be added to the
get
andset
methods. These functions transform or interact with the data before returning or applying it. -
Change Signals: The module includes a
changed
signal, similar to Roblox signals, to notify listeners of value changes. -
Immutable and Public Interfaces:
-
immutable
: Provides a read-only interface withget
andchanged.Restricted
. -
public
: Provides a more limited interface withget
,set
, andchanged.Restricted
.
-
Usage
Creating a New Prop
local new_prop = require(path.to.props)
local prop = new_prop(10) -- Creates a prop with an initial value of 10
Using Middleware
local function double(n : number)
return {
value = n*2,
cancel = false
}
end
local x = new_prop(1)
x.middleware.set.add(double)
x.set(3)
print(x.get()) -- 6
Accessing the Prop
local value = prop.get() -- Gets the current value
prop.set(20) -- Sets the value to 20
Listening for Changes
prop.changed:Connect(function(newValue, oldValue)
print("Value changed from", oldValue, "to", newValue)
end)
Using Immutable and Public Interfaces
local immutable_interface = prop.immutable
local public_interface = prop.public
local value = immutable_interface.get() -- Gets the value (cannot set it)
public_interface.set(30) -- Sets the value
This module is not battle-tested and this is the release version of it, so it’s naturally prone to being flawed. If you come across any issues when using the module please let me know.