I am currently trying to make a network of modules for my game in order to keep it organized, but I am having a lot of issues with recursively requiring them right now. I ran into a specific workaround, but I am unsure if there are any issues with it.
I saw in another post that you could just require the module inside the function it is needed in, but I am unsure if there are any performance issues that come with this. I was specifically wondering if Luau has a stack feature like java where everything in the function is wiped from the stack when the function finishes, including the module that was required inside it, basically making sure there are no long term performance issues.
Yeah, if the variable is localized within the function, it’s going to be garbage collected once the function runs through. If you’re intending to access the module later on, I would recommend:
local ModuleThatWillRecursivelyLoad --// Keep this nil, fill in later
local SomeOtherModule = require(PathToOtherModule) --// This is just a proof of concept
local module = {}
function module.Initialize()
if not ModuleThatWillRecursivelyLoad then
ModuleThatWillRecursivelyLoad = require(PathToModule)
end
--// Any other initialization code you might have
end
return module
In this way, both ModuleThatWillRecursivelyLoad and SomeOtherModule are going to be equally available to you once you initialize your module.
No, it wouldn’t and it’s only requiring the module once.
For example, this is how I structure modules in my framework.
--// This is my module 'SomeService'
local ModuleThatWillRecursivelyLoad --// Keep this nil, fill in later
local SomeOtherModule = require(PathToOtherModule) --// This is just a proof of concept
local Initialized = false
local module = {}
function module:SomeMethod()
if not Initalized then
--// you could either call Initialize directly here or just return - it depends on how vital the rest of your initialization is to the module's function
end
--// Method code
SomeOtherModule:SomeMethodWithinThisOtherModule() --// I can safely call methods for this module, because I know 'SomeService' (this module) has been initialized already
end
function module.Initialize()
if Initialized then
return
end
Initialized = true
if not ModuleThatWillRecursivelyLoad then
ModuleThatWillRecursivelyLoad = require(PathToModule)
end
--// Any other initialization code you might have
end
return module
When I require this module (SomeService) I immediately call SomeService.Initialize(). For my framework, I have initialization methods in all of my modules, because there’s usually some code I want to execute (ex: Remote connection, requiring modules to resolve recursive requiring, PlayerAdded events, etc.)
To get to what you are asking, in both the earlier code snippet and this one above (which was provided just to give a more realistic/thorough look of this concept in action), I’m only requiring the other module once (when I call ‘.Initialize’). I generally tweak my .Initialize function to only allow it to run once (as shown in the second snippet - once it runs, the variable Initialized ticks true and the function won’t execute no matter how many more times you call it).