How would you use .new() in a module script to create a new gui object and edit its properties

k so basically
How would you use .new() in a module script to create a new gui object and edit its properties? for instance, I’ve seen this in UIShelf, and have been wanting to replicate this for my new and improved notification module.

the example script for uishelf looks like this:

local UIShelf = require(script.Parent:WaitForChild("MainModule"))

local NewButton = UIShelf.new()

NewButton:setImage("rbxasset://textures/ui/SearchIcon.png")
NewButton:setLeft()
NewButton:setOrder(2)
NewButton:setName("SearchIcon")
NewButton.MouseClick:Connect(function()
   print("Button clicked")
end)

btw you can find the link to my old notification module here if you wanna use it

It just uses the “object” oriented approach (object being in parentheses because it isn’t actually object-oriented, it just mimics a prototype, look here for a more in-depth explanation and here for official Lua documentation on the idea of classes) to create a table with a metatable that points to a library of class functions, that when that object’s methods are called, updates a property of an instance actual instance.

local someClass = {}
someClass.__index = someClass -- needed for method inheritance

function someClass.new(realObject: Frame)
    return setmetatable({ frame = realObject }, someClass)
end

function someClass:setPosition(position: UDim2)
    self.frame.Position = position
end

Then just call someClass.new():setPosition(UDim2.new(0.5, 100, 0.5, 100)

If you’re looking to actually wrap and change an instance’s behaviour though, you need a wrapper. But I suppose in that case when you’re making an object that edits an instance, you’re sort of creating a wrapper. But I digress, here’s a good tutorial once you understand what metamethods are and how they work.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.