Something like this where I don’t have to put all my methods and constructor function inside a single module script
Yes, you can. A modulescript can return a function. However, you can’t use the colon syntax when declaring the functions, so remember to put a self parameter in the functions that are methods. Don’t put a self parameter in constructors, though. Although you can’t define the functions using a colon, you can still call the method with colon syntax, of course.
Main class module
local ToolClass = {}
ToolClass.__index = toolClass
ToolClass.Method1 = require(script.Method1)
ToolClass.Method2 = require(script.Method2)
a module containing a method
return function(self, --[[other parameters]])
end
a module containing a constructor
return function(--[[parameters]])
end
yeah you can require all modules at once if needed. but you may need to create shortcut functions for convenience. (and could lead to unoptimized work, cyclic table errors and time complexity could get crazy after awhile, depending on your design)
local module1 = {
Module2 = require(script.Module2), --has functions for printing
Module3 = require(script.Module3) --has functions for adding
}
-- OPTIONAL shortcut functions
function module1:Print(msg)
self.Module2:Print(msg)
end
function module1:Add(n, n2)
return self.Module3:Add(n,n2)
end
return module1
isn’t truly my favorite design, but i’ve seen a lot of frameworks require TONS of modules at once and used a single module to unify them in many different ways, so just gotta be creative i suppose.
EDIT: in languages like this you can still just call the whole module a: Module, Library or Package.
it would take a bit more work and features before being considered a framework imo.
@RoBoPoJu
Is their any more resources on understanding this part
return function(self, --[[other parameters]])
end
Also I’m wondering if what I wrote is okay.
--Server Script
--Services
local rs = game:GetService("ReplicatedStorage")
--Classes
local IceballClass = require(rs:WaitForChild("IceballClass")) -- Metatable that only includes the constructor
local UpdatedClass = require(rs:WaitForChild("IceballClass").UpdatedClass) -- New metatable that includes the contructor plus a new method
--Object
local IceballObject = IceballClass.new() --
setmetatable(IceballObject, UpdatedClass) --Sets the metatable to the updated metatable, which contains the constructor and the method
IceballObject:FirstMethod() -- Calls the method, metamethod in the updated metatable runs which redirects it to the updated class metamethod which contains the method.
--New Class
--Services
local rs = game:GetService("ReplicatedStorage")
--Classes
local oldIceballClass = require(rs:WaitForChild("IceballClass"))
function oldIceballClass:FirstMethod()
print("run the first method")
print(self.Projectile)
end
return oldIceballClass
What I did was just copy the old metatable and add a method to it but whenever I wanted to add a new method I would have to update the metatable. Which way do you recommend?
Methods in diffrent scripts.rbxl (21.9 KB)
You seem to do this in a more complicated way than necessary. I meant that your main class module script should require the modules that create the methods and constructors. In the code you posted you are instead requiring the main class module in a module that creates a method.
The modulescripts where you define the functions don’t need to set them as methods of the class. They should instead return the functions to the main class module which should then set them as methods for the class.