So, this isn't so much a helper function, but rather a -style- for organizing code modules that may be helpful to people. When you want to do a kind of "hard reset" for a game, a neat way of doing it is to call love.load again. When you load everything from there, it kind of makes it easy, following a basic assumption that you put all of the loading code there. So, here's a way to organize modules in code for that purpose:
Code: Select all
-- In this style, assume that we're loading modules that return a table of functions,
-- and we will put them in the global state rather than locally to every where they're needed.
-- Because of dependencies, ordering is important and we can't use a regular table.
-- Each subtable is { global name, module filename }
game_libraries = {
{ "Graphics", "lib.graphics" },
{ "Audio", "lib.audio" },
{ "Sprite", "lib.sprite" },
{ "Game", "lib.game" },
}
function load_libraries()
for _, v in ipairs(game_libraries) do
local global = v[1]
package.loaded[global] = nil
_G[global] = nil
end
garbagecollect("collect") -- give all finalizers a chance to run
for _, v in ipairs(game_libraries) do
local global, filename = v[1], v[2]
_G[global] = require(filename)
end
end
function love.load()
load_libraries()
Graphics.init()
Audio.init()
Game.init()
end
If you keep everything clean and don't litter your global table, this works out pretty nicely.