Homebrew (and SIMPLE) Module Loader?

Hey so, I am developing a little project and this time around I have decide to not use any framework such as Aero because I want my code to be portable and not dependant on a Framework, code developed in Aero for the most part needs to have Aero to run, so …

On of my favorite things about Aero was the module loader, being able to call things without requiring manually them in every single script. So … (again)

Does anyone have a simple module loading module that i can look at for this purpose?

Thanks! :slight_smile:

2 Likes

If you don’t want them to call them manually then store them in _G (which is highly unrecommended for security reasons) or have a single module that acts as a proxy towards using the other modules that you want, and require all the modules in that proxy module.

1 Like

yeah thats actually what I was asking for someone to show me an example of.

A single module that I can require and call other modules within it easily, similar to how Nevermore works, but without all the stuff that Nevermore comes with.

Wondering if anyone has something like that I can look at?

:slight_smile:

Your master module could be very simple.
Master module:

local Modules  = {}
Modules.ModuleA = require(ModuleA)
Modules.ModuleB = require(ModuleB)
return Modules

Anything else:

Gameplay = require(MasterModule)
Gameplay.ModuleA.FunctionOne()
1 Like

I made a simple one that just gets all the module scripts from certain locations and stored them in a dictionary, then I require it by name, here are the modules:

--Import module:
local RunService = game:GetService("RunService")

local ModuleLoader = require(script.ModuleLoader)

if RunService:IsServer() then
	
	return ModuleLoader.New({
		game.ReplicatedStorage.Modules.Shared,
		game.ServerStorage.Modules
	})
	
elseif RunService:IsClient() then
	
	return ModuleLoader.New({
		game.ReplicatedStorage:WaitForChild("Modules"):WaitForChild("Shared"),
		game.ReplicatedStorage:WaitForChild("Modules"):WaitForChild("Client")
	}) 
	
else
	assert("Unknown environment")
end

And the module loader class:

local ModuleLoader = {}
ModuleLoader.__index = ModuleLoader

function ModuleLoader.New(FolderArray)	
	local self = setmetatable({
		_FolderArray = FolderArray,
		_NameToModuleMap = {}			
	},ModuleLoader)
	
	self:_CreateNameToModuleMap()
	
	return self
end

function ModuleLoader:_CreateNameToModuleMap()	
	for _, Folder in ipairs(self._FolderArray) do
		for _, Descendant in ipairs(Folder:GetDescendants()) do
			if Descendant:IsA("ModuleScript") then				
				assert(self._NameToModuleMap[Descendant.Name] == nil, "More than one module script is using the name : " .. Descendant.Name)
				self._NameToModuleMap[Descendant.Name] = Descendant		
			end		
			
		end
	end	
end

function ModuleLoader:__call(Name)
	assert(self._NameToModuleMap[Name], "Invalid module script name : " .. Name)
	return require(self._NameToModuleMap[Name])
end

return ModuleLoader

There is also a check to ensure that you don’t have two module scripts with same name. To use it would just do:

local Import = require(--path to the import module)

local SomeModule = Import("SomeModule")--Name of module in string

It works perfectly for me so far, and I love it since I’m always changing the folders where my modules are located, for organisational purposes, and saves me the pain of having to update the path every time.

6 Likes