I need my modules to be able to access each other without long and complicated paths. Hence, metatables are the obvious solution. However, this is the issue I’m running into:
This is the code that “loads” each module. For testing, I turned __index into a function rather than a table of the other modules. The issue that I’m having here is that the metatable is not set until after the module is required. Therefore paths cannot be defined at the top of my code. Here is what I’m talking about:
local module = {}
print(module.Foo) -- This does not print "Test" as previously defined in my other code sample because the metatable has not been set yet.
return module
local module = {}
spawn(function()
wait() -- This does print "Test" because the metatable is set by the time wait() is completed in this new thread
print(module.Test)
end)
return module
I could always just define the paths within each function, but that kind of defeats my original intentions of removing long paths to other modules and is repetitive.
Does anyone have any suggestions to how I can work around this (besides the bandaid solution I posted above)? Kind of stuck atm.
Not sure what you’re expecting to happen here? Lua runs off of a single thread (typically, granted there are ways to run semi-asynchronously, coroutine library, etc…), and therefor everything is linear.
You require the module > the module runs the code within it > you set the metamethods.
The majority of frameworks (if not all), which in theory is what you’re sort of attempting to make here, tend to have starting functions. Keep in mind when using ModuleScripts it’s good habbit to always define functions and execute them, rather then executing code on initialization of the module script.
An example of a ‘start’ function would be as follows.
local ModuleScript = {}
local path1, path2;
function ModuleScript:Start()
path1, path2 = ... -- define paths properly (ex; self.path1, self.path2)
end
return ModuleScript