Last active
November 30, 2021 04:21
-
-
Save Zbizu/4f85fdda0b6a2052243c92bfe1c98125 to your computer and use it in GitHub Desktop.
Lua module system concept
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- module system | |
if not Game.modules then | |
-- load | |
Game.modules = { | |
moduleList = {}, | |
unLoadedModuleList = {} | |
} | |
else | |
-- reload | |
local newModuleList = {} | |
for moduleName, moduleData in pairs(Game.modules.moduleList) do | |
if moduleData.enabled then | |
Game.modules.callModuleFunction(moduleName, "onUnLoad") | |
else | |
newModuleList[moduleName] = nil -- disabled | |
end | |
end | |
Game.modules.moduleList = newModuleList | |
end | |
function Game.modules.registerModule(module) | |
if Game.modules.moduleList[module.name] and not Game.modules.moduleList[module.name].enabled then | |
return | |
end | |
Game.modules.moduleList[module.name] = module | |
Game.modules.callModuleFunction(module.name, "init") | |
Game.modules.callModuleFunction(module.name, "onLoad") | |
return true | |
end | |
function Game.modules.disableModule(moduleName) | |
if Game.modules.moduleList[moduleName] and Game.modules.moduleList[moduleName].enabled then | |
Game.modules.callModuleFunction(moduleName, "onUnLoad") | |
Game.modules.unLoadedModuleList[moduleName] = Game.modules.moduleList[moduleName] | |
Game.modules.moduleList[moduleName] = nil -- disabled | |
return true | |
end | |
end | |
function Game.modules.loadModule(moduleName) | |
if Game.modules.unLoadedModuleList[moduleName] then | |
Game.modules.moduleList[moduleName] = Game.modules.unLoadedModuleList[moduleName] | |
Game.modules.callModuleFunction(moduleName, "onLoad") | |
Game.modules.unLoadedModuleList[moduleName] = nil | |
return true | |
end | |
end | |
function Game.modules.unLoadModule(moduleName) | |
if Game.modules.moduleList[moduleName] and Game.modules.moduleList[moduleName].enabled then | |
Game.modules.unLoadedModuleList[moduleName] = Game.modules.moduleList[moduleName] | |
Game.modules.callModuleFunction(moduleName, "onUnLoad") | |
Game.modules.moduleList[moduleName] = nil | |
return true | |
end | |
end | |
function Game.modules.callModuleFunction(moduleName, address, ...) | |
if Game.modules.moduleList[moduleName] and Game.modules.moduleList[moduleName].enabled and Game.modules.moduleList[moduleName][address] then | |
return Game.modules.moduleList[moduleName][address](...) | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- sample module | |
local module = { | |
name = "sampleModule", | |
version = "1.0", | |
author = "Zbizu", | |
URL = "https://github.com/Zbizu", | |
enabled = true, | |
description = "This is a sample module." | |
} | |
module.init = function() | |
-- run once on startup | |
local ec = EventCallback | |
ec.onLook = function(self, thing, position, distance, description) | |
return Game.modules.callModuleFunction(module.name, "exampleMethod", self, thing, position, distance, description) or description | |
end | |
ec:register() | |
end | |
module.onLoad = function() | |
-- run on startup and on every reload | |
end | |
module.onUnLoad = function() | |
-- run when module is about to be unloaded | |
end | |
module.exampleMethod = function(self, thing, position, distance, description) | |
if self:getGroup():getAccess() then | |
return description .. "\nThis text comes from sample module. Only access players can see it. Go to data/scripts to disable." | |
else | |
return description | |
end | |
end | |
Game.modules.registerModule(module) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment