Hello, I’m trying to learn how metatables work. But, when trying to link Frame object to it, and change value of metatable, I understood that IDK how I can reflect that change to frame:
local Object = {}
Object.__index = Object
function Object.new(Size, Position, Rotation...)
local NewObject = {}
NewObject.Size = Size
NewObject.Position = Position
NewObject.Rotation = Rotation
Frame2D = Instance.new("Frame")
Frame2D.Size = Size
Frame2D.Position = Position
Frame2D.Rotation = Rotation
NewObject.Frame = Frame2D
return setmetatable(NewObject, Object)
end
...
local function changer(Object, newValue)
Object.Position = newValue
-- Object is METATABLE and not roblox instance.
-- I want change Frame2D position to changed metatable position.
end
How I can detect, when I try change smth in metatable, if that key already exists, without additional Object.Frame.Position = newValue?
So did a lil bit of digging and I realized that __newindex only fires when an index that is not present in the main table changes. Since, you set Position in NewObject table and assign the __newindex metamethod to that table, changes to Position won’t be fired as it is an exisiting key in the table. An ugly work-around is to set the Position property after setting the metatable.
local obj = setmetatable(NewObject, Object)
obj.Position = Position
return obj