Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How detect element change in table, without using SetMethods()?

Asked by 7 years ago

I'm basically creating my custom GuiLibrary, that has its own Gui elements(they still use roblox original gui elements, im just creating wrapper around them with more functionalty) and they have their own methods and functions and different properties, just like roblox Gui elements have.

In the Module Script code i have "obj.Size" table with parameters, i can detect change in obj table through __newindex, but how would i go about detecting change inside obj.Size table? If i change for example: obj.Size.x i want to fire the bindableEvent i have inside my obj table. So that's basically the problem is, i can detect variable change inside obj table, but not in obj.Size table, how would i go about this?

Module Script code:

local module = {}


function module.new(o)
    local obj = {}
    obj.Changed = Instance.new('BindableEvent')
    obj.Name = 'ScrollableFrameName'
    obj.Size = {x = 50,y = 100}

    obj.mt = {}

    obj.mt.__index = function(tbl,key)
        return obj[key]
    end


    obj.mt.__newindex = function(tbl,property,value)
        --print('Property'..property..' changed to: '..value)
        obj[property] = value
        obj.Changed:Fire(property,value)
    end

    local proxy = setmetatable(o or {},obj.mt)
    return proxy
end

return module

Regular Script code:

local ScrollableFrameModule = require(script.Parent:WaitForChild('ScrollableFrame'))
local ScrollableFrame = ScrollableFrameModule.new()

ScrollableFrame.Changed.Event:connect(function(property,value)
    print('Changed property:'..property..' to '..value)
end)

ScrollableFrame.Size.x = 999
ScrollableFrame.Name = '123'
1
You have to do the same `__newindex` trick you did for the `ScrollableFrame` for the `Size` Table. adark 5487 — 7y

1 answer

Log in to vote
0
Answered by 7 years ago

In the same way that you do "module.new", you must also have a "size.new". Use this new size type when you initialize the size on line 8.

Also, I am fairly certain that you can simplify lines 12-14 to just obj.mt.__index = obj if you don't want any custom behaviour there. Since obj is a table, lua will know to look through the table for the key (which is all you're having it do now).

Ad

Answer this question