It's not dumb at all, though it depends on what you're trying to accomplish. Here's a few models. For the sake of simplicity, I'm going to assume each module modifies the global table which is fine for game-specific code.
1. Initialize Module when Requiring it
Code: Select all
-- other.lua
other = {}
function other.doSomething()
--
end
do
-- Module initializing code here
end
The trick to this is that the modules have to be required in order of dependencies. Meaning, if the Foo module uses code in the Bar module, then Bar has to either be required first, or in Foo there needs to be a require("Bar") line. This is completely normal and most people do this. Circular Dependencies are a headache to deal with, though.
2. Modules require explicit initialization
Code: Select all
-- other.lua
other = {}
function other:init()
--
end
function other:doSomething()
--
end
In this model, which yours is like, the modules don't spark a whole load of execution from a require statement. This is also pretty normal, and Libraries tend to take on this pattern. 'init' is the convention, but 'load' is also fine. Order the require lines aren't as important, and you don't really need to put the requires in each module. The init calls do matter. In the previous example, Bar:init would have to be called before Foo:init. Also, putting all of those init calls in the love.load function makes it easier to reset the game.
3. Super Clean Reusable Module Ninja Library Code
This is how library writers do their code. Do this if you want to share code with others:
Code: Select all
-- other.lua
local other = {}
function other:init()
--
end
function other:doSomething
--
end
return other
This one does NOT modify the global table. For library writers that's taboo. For instance, lets say you needed code from Mumbly Joe's util.lua, and you needed code from Super Steve's util.lua, and both modified the global table 'util'. Then you have a namespace collision, and have to directly edit the files. Instead, the module that requires the library module is responsible for the global table. See:
Code: Select all
-- main.lua
steves_util = require('3rdparty.steve.util')
joes_util = require('3rdparty.joe.util')
function love.load()
steves_util:init()
joes_util:init()
end
One thing to point out about the code I've been writing, I've been using the colon (:) operator to create an implied self in the module functions. Others will disagree with me on this, and say you should use the dot (.) operator. They can chime in with what they think, and they're probably right. My opinion is that the point of the colon operator is to signal that the functions are going to access modify the table they're apart of, so it makes sense when the module is serving as a Singleton.