UI • Client Logic • Effects • Smooth Systems
local Signal = require(game.ReplicatedStorage.Packages.Signal)
local State = {}
State.__index = State
function State.new(initial: T)
local self = setmetatable({}, State)
self.value = initial
self.changed = Signal.new()
return self
end
function State:set(v)
if v ~= self.value then
self.value = v
self.changed:Fire(v)
end
end
function State:get()
return self.value
end
-------------------------------------------------------
local Runtime = {}
Runtime.__index = Runtime
function Runtime.new()
return setmetatable({
bindings = {},
mounted = false,
}, Runtime)
end
function Runtime:bind(state, guiObj, property)
table.insert(self.bindings, state.changed:Connect(function(v)
guiObj[property] = v
end))
guiObj[property] = state:get()
end
function Runtime:mount()
self.mounted = true
end
-------------------------------------------------------
-- usage
local runtime = Runtime.new()
local HealthState = State.new(100)
local frame = script.Parent.Frame
runtime:bind(HealthState, frame, "BackgroundTransparency")
runtime:mount()
task.spawn(function()
while true do
HealthState:set(math.random())
task.wait(0.1)
end
end)