Building a table of required module scripts? (using Knit "framework")

I know module scripts cache, so I am considering building a table when the game starts up that has all of my commonly used module scripts required into that one table so i can easily call functions from anywhere.

Like so:

local playerPoints = TableOfModules.PointCalculator.GetPoints(player)
local playerMoney = TableOfModules.MoneyCaluculator.GetMoney(player)
etc.

There might be a total of 20-50 modules in there eventually.

I know modules cache when required, is there a memory issue defining that table of modules in various scripts across my game?

Since the modules are all cached, i am not sure there is a memory issue with this.

a module

return {
    PointCalculator = require(PointCalculator),
    MoneyCalculator = require(MoneyCalculator)
}

the script you showed

local TableOfModules = require(TableOfModules)

local playerPoints = TableOfModules.PointCalculator.GetPoints(player)
local playerMoney = TableOfModules.MoneyCaluculator.GetMoney(player)

something like this should be fine, I don’t think memory should be an issue
someone can correct me if I am wrong

1 Like

Yes, to be more specific i am using Knit by @sleitnick which can be found here: GitHub - Sleitnick/Knit: Lightweight game framework for Roblox

Here is my actual code, this is run once when the server starts up. Knit is the table, it will contain all services and now, with this format, also contain all the modules required in this way.

-- Knit Runtime

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerScriptService = game:GetService("ServerScriptService")
local Knit = require(ReplicatedStorage:FindFirstChild("Knit",true))

-- Expose Modules to Knit
Knit.Powers = {}
for i, v in pairs(ReplicatedStorage.RojoSynced.PowerModules:FindFirstChild("Powers"):GetChildren()) do
    if (v:IsA("ModuleScript")) then
        Knit.Powers[v.Name] = require(v)
    end
end

Knit.Abilities = {}
for i, v in pairs(ReplicatedStorage.RojoSynced.PowerModules:FindFirstChild("Abilities"):GetChildren()) do
    if (v:IsA("ModuleScript")) then
        Knit.Abilities[v.Name] = require(v)
    end
end

-- Load all services:
Knit.AddServices(script.Parent.Services)

Knit.Start():Catch(warn):Await()

Knit is then used throughout the game by simply doing this line:

local Knit = require(ReplicatedStorage:FindFirstChild("Knit",true))

Everything in the table, including the services loaded and the modules loaded in the way i have shown here, are usable by simply addressing it like you would any values in a table.

Knit will be loaded into nearly every script in the game, with this entire table coming with it. I guess my question still stands, does this bloat memory or are all these modules just loaded once into the Knit table.

If you dont know Knit, just think of it as a big table of module scripts.

Hello, I couldnt understand your post correctly so correct me if I am wrong but from what I have understand you are trying to dump every commonly used modules to a table so then you can call them anywhere. Couldnt you do this by using the Knit.GetService function(or GetController for client) ?

local pointcalculator = Knit.GetService("PointCalculator")
local moneycalculator = Knit.GetService("MoneyCalculator")

pointcalculator.GetPoints()
moneycalculator.GetMoney()

You can also use the knit services table;

local TableOfModules = Knit.Services

local playerPoints = TableOfModules.PointCalculator.GetPoints(player)
local playerMoney = TableOfModules.MoneyCaluculator.GetMoney(player)

To note: it would be a bad idea to put modules into the Knit Services table, they are handled differently within Knit.

To keep this post being too much about Knit, I just really want to know if I am going to bloat memory by requiring all my modules into one table, then required that table across the game in nearly every script.

I guess I don’t understand fully how modules are cached, are they cached globally or within each scripts environment?

Couldn’t you just run the Knit.AddServices with your commonly used modules and use them like a regular Knit service as I explained in my previous post? I am probably wrong here, since there is no way that you havent thinked about loading your modules as a regular knit service. Isn’t the purpose of Knit is to be the place where all of your modules will be, ie. TableOfModules in your situtation.

You can also do something like you create Knit.Modules table in your knit initialization then you dump all of your commonly used modules there looking something like this in your regular script;

local TableOfModules = Knit.Modules

local playerPoints = TableOfModules.PointCalculator.GetPoints(player)
local playerMoney = TableOfModules.MoneyCaluculator.GetMoney(player)

I will admit it that I have no idea if modulescripts are cached or not, in my games I dump all of my modules into one _G table and use it from there, I dont have to worry about caching since _G is just one table. However, I havent been able to get the Luau autocomplete feature on it and if you have the _Gphobia it is probably not the solution for you. You can also create a TableOfModule modulescript looking like this utilizing the _G technology and not having to worry about caching;

if _G.modules == nil then
 _G.modules = {points = require(game.replicatedstorage.points)}
end
return _G.modules

its a bad idea to add modules that are not services to the Service table of Knit, so if anyone ever reads this: DONT DO THAT.

Thanks for your thoughtful responses, but what i’m looking for is related to my examples above and if they will bloat memory.

Knit.Modules might be a internal part of Knit Framework, but you can change it to something like Knit.Modules2 to solve the problem, by Knit.Modules I didnt mean that you are going to write to a internal part of Knit but rather meant that it could be any table name that would store your commonly used modules so I used Knit.Modules to point it out.

What I am trying to tell you is that you probably dont need TableOfModules table at the first place. You can add them to the Knit as a service then use them like a regular knit service. If you are still against the idea of putting every module to the Knit at the first place then you can create a Knit.Modules2 table and dump all of your commonly used modules there and reference them from there