I want to call a function on a function. Heres an example so you know what I mean and what I want to do.
module.new():SetTitle()
-- One more question: Is there a way that if I declare a function on the 'new' function, I could somehow set the name of that new object that was created?
That’s really simple, All you need to do is to create a Class and use the method on it. Some classes have “chains” which means that you can call how many methods as you want in chain and it’ll always work. (You need to understand of how OOP, Oriented Object Programming, works to achieve that) Here’s a small example of how you would achieve that:
-- ModuleScript Code
local MessageClass = {}
MessageClass.__index = MessageClass
MessageClass.__type = "MessageClass"
function MessageClass.new()
return setmetatable({}, MessageClass)
end
function MessageClass:PrintMessage(...)
print("[MessageClass]:", tostring(...))
return self -- Returns `self` (Class) so we can chain methods!
end
function MessageClass:WarnMessage(...)
warn("[MessageClass]:", tostring(...))
return self -- Returns `self` (Class) so we can chain methods again!
end
return MessageClass
-- Script code
-- Gets the Class and Constructs a new one
local MessageClass = require(script.MessageClass)
local CustomMessageClass = MessageClass.new()
-- Now we can call methods in chain!
CustomMessageClass:PrintMessage("Hello, World!"):WarnMessage("Hello, Again, World!")
So I would basically do whatever I want do with the function here:
function UIShelf.new()
-- right here
return setmetatable({}, UIShelf)
end
Also, how would I do something like this UIShelf.new():SetName("Test")? Like have it actually set the name of the new instance without declaring the parameter in new() or SetName()? Example:
function UIShelf.new()
local newObj = Instance.new("Part")
return setmetatable({}, UIShelf)
end
function UIShelf:SetName()
-- set name of newObj?
end
You need to use the .new() constructor as it’ll create a new Class. Not using it will probably raise an error if you used the self keyword on other methods, Such as retrieving a certain value or checking/modifying it.
Also, When adding new Objects into your Class, You must reference them, Such as Class.Object = {} (Or like i did below)
-- Script
local UIShelf = require(script.UIShelf)
local TestUI = UIShelf.new()
TestUI:SetName("Test") -- Works
UIShelf:SetName("Test") -- Doesn't works
-- ModuleScript
local UIShelf = {}
UIShelf.__index = UIShelf
UIShelf.__type = "UIShelf"
function UIShelf.new()
local Class = setmetatable({}, UIShelf)
Class.Instance = Instance.new("Part")
return Class
end
function UIShelf:SetName(Name)
self.Instance.Name = Name
return self
end
return UIShelf
That’s optional. If you want to chain methods, Always return self. It’s really up to you, You can also return anything else you want to, Such as get the Class’ Instance!
function UIShelf:GetInstance()
-- Now we can directly modify the Instance!
-- UIShelf:GetInstance().Color = Color3.fromRGB(255, 255, 255)
return self.Instance
end