Structure:
How would I structure my project to be modular?
– What code practices should I follow?
Dependencies:
Whats the best way to manage dependencies in an organized way?
– I’m going to have a lot of “shared” modules that all of my scripts can access.
– In a lot of my scripts I will have to dedicate 3-7 lines just to locate the module.
Dependency Example:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Classes = ReplicatedStorage.classes
local Shared = RelicatedStorage.shared
local module = require(Shared:WaitForChild("myModule"));
local class = require(Classes:WaitForChild("myClass"))
This is fine, Although after a while this becomes very messy because of the many things to require(). Even before I start coding the main part of the script.
What I have tried:
I understand you can load modules to _G, or shared.
(shared and _G are the same thing)
Example:
_G[module.Name] = require(module)
And to access the module(s) methods:
_G.module.foo()
Is this the only way not to have to require many modules? Or is there a more graceful way of doing this?
Thank you, any corrections or suggestions are appreciated.
Interesting supposition. I am in no position to offer any advice on this, the question alone gave me pause to realise you know more than me about this, in fact you have showed me something that at some point will probably change the way I code in Lua forever. I will say this though, that the _G method is not very humanly understandable or readable, and you may find yourself chasing your tail to find the original declaration of this stuff when you are trying to do something in a hurry. I know I do, with my own code and quite often. Trying to find the original source of a declaration (with convoluted path dependencies) can be difficult after spending enough time not staring at the code. Whereas your dependency example, it is instantly clear where all the stuff is, and it might save time when you are trying to pull something quickly from your code-base. And for the sake of 3-7 lines it is something that may save you a lot of time in the long run. Just an opinion that is mine, like I already said, this is probably something than when I fully understand the wealth of I will incorporate into my own coding style. As a teaching method, someone who is trying to learn the basics should not be exposed to it too early, abstraction can be a distraction. For now, I am an amateur in Lua, flicking around the forums shows me just how far I have to go.
One thing alone has blown my mind, being someone that loves C++, it is this:
spawn(function("anything you care to pass, doesn't matter how much!")
end)
And this is something you can call anywhere, even inside function declarations!!!. Blows my mind, I would love to see the architecture that made this possible…