If the title isn't really clear...
Let's say I have Window.new() - built with a Frame, and a bunch of other TextLabels, TextButtons, etc. - and I want to edit its Size property. Just like ordinary Frame objects, I want the new Window to resize as soon as its custom Size property is changed. But because the Window class has custom properties, specifically the Size property, that initially had no control over the size GUI, I need to know how I could mitigate this.
I, unfortunately, have not enough relevant code to show, and as far as my research with custom classes go, I can't seem to find the info I need, so I am currently clueless. My only guess is using __index metamethod, yet I still do not know how I should implement it.
Do I really have to use the __index metamethod for this? And if so, how?
Thanks in advance.
No, you shouldn't need the __index meta method for this. You could, instead create a new BindableEvent and whenever the custom size property is changed, fire this BindableEvent. The only downside is that you would have to use a custom function or a proxy table to know when the custom Size property is changed. The proxy table would use __index and the __newindex meta methods though.
01 | local window = { } |
02 | function window.new() |
03 | local newWindow = { } |
04 | local internalEvent = Instance.new( 'BindableEvent' ) |
05 | newWindow.SizeChanged = internalEvent.Event -- assigns the BindableEvent's Event event to the .SizeChanged property/event |
06 | newWindow.Size = UDim 2. new( 1 , 0 , 1 , 0 ) |
07 | -- you can choose this method, a proxy table |
08 | local newWindow = setmetatable ( { } , { |
09 | __index = newWindow; |
10 | __newindex = function (self, index, value) |
11 | if index = = 'Size' then |
12 | internalEvent:Fire() |
13 | rawset (newWindow, 'Size' , value) |
14 | else |
15 | return newWindow |