local Widget = {}
Widget.__index = Widget
function Widget.new(Class: any)
Class = Class or Widget
local data = {}
local proxy = setmetatable({}, {
__index = function(_, Key)
if Class.Getters and Class.Getters[Key] then
return Class.Getters[Key](data)
end
return rawget(data, Key)
end,
__newindex = function(_, Key, Value)
local Old = data[Key]
data[Key] = Value
if Class.Setters and Class.Setters[Key] then
Class.Setters[Key](data, Value, Old)
return
end
rawset(data, Key, Value)
end,
})
return proxy
end
function Widget:Setter(property: string, callback: () -> any)
if not self.Setters then
self.Setters = {}
end
self.Setters[property] = callback
end
function Widget:Getter(property: string, callback: () -> any)
if not self.Getters then
self.Getters = {}
end
self.Getters[property] = callback
end
--[[
Label:
Inherits from Widget class
]]
local Label = setmetatable({}, Widget)
Label.__index = Label
Label:Setter("CustomProperty", function(data, value)
if data.Object then
data.Object.Name = value
end
end)
function Label.new()
local self = Widget.new(Label)
self.Object = Instance.new("Frame")
self.Size = 100
return self
end
local l = Label.new()
l.CustomProperty = "Hello lol"
l.Parent = game.Workspace
print(l.Name, l.Parent)
The results are always nil, Workspace.
I'm trying to figure out how to have my own custom properties that map to a function (callee)
The function would be responsible for setting eg, self.Object.Name to x.