#Why doesn't this work (custom properties on an inherited class)

1 messages · Page 1 of 1 (latest)

quick finch
#
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.

cosmic ridge
#

this is perfect use of ai btw

#

try it

quick finch
cosmic ridge
#

it is

quick finch
#

If you don't have anything to say to help me then don't say anything

#

I'd rather get help from somebody who has an understanding of this stuff rather then something that guesses

cosmic ridge
#

you are very ignorant

quick finch
#

ok please stop responding

modest kernel
#

vro is overcomplicating ts

granite tundra
# quick finch ```lua local Widget = {} Widget.__index = Widget function Widget.new(Class: an...

lowkey im too much of a dumbass to understand ur code but you can do inheritance like this not sure why we need proxy tables:

local Car = require(script.Parent.Car)


local Truck = {}
Truck.__index = Truck

function Truck.new()
    local self = setmetatable(Car.new(), Truck) -- instead of using empty table use the instance
    
    self.Gas = 200
    self:Refill()
    
    return self
end

function Truck:Refill()
    self.Gas += 50
end

setmetatable(Truck, Car)

return Truck
  1. you just gotta make self the car.new() instance so that the inherited class gets their fields
  2. then you just gotta set the Truck metatable to car so that if we dont find a metatable inside truck we can look inside car for it

This guide is pretty good: https://devforum.roblox.com/t/guide-to-type-checking-with-oop/1997394

quick finch
granite tundra
#

cause the AddPropertyBinding stuff seems like overkill l

quick finch
#

well

granite tundra
#

well if u just want the frame to inherit properties from widget you can just put those properties in self

#

so like you do:

local self = setmetatabel({},widget) --this is the widget

local self = setmetatable(widget.new(),frame) -- this is the frame see how we use the widget.new as self?

#

and if u got any custome properties on the frame u can just add them by doing self.whatever so the frame has the custome properties and the properties in widget

modest kernel
#

ur complicating it

quick finch
granite tundra
# quick finch no I'm not

I dunno bro this property binding stuff seems unneeded for that you are trying to do maybe Im misinterpreting what ur trying to achieve?

quick finch
#

Following Qt's hierarchy

#

I understand the principle

#

so if I ever have the need, to do the binding stuff again (probably not)

#

I can do it just like how I planned it

granite tundra
#

👍 goodluck!