Plugin Discovery and Loading
Scan directories for plugin files and load them with require or loadfile.
Plugin Discovery and Loading is a free Lua Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Lua Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What Is a Plugin Architecture?
A plugin architecture allows third-party code to extend an application without modifying its core. Lua's require and dynamic loading make this natural.
Plugin Discovery by Convention
Scan a directory for *.lua files matching a naming convention, then load each as a plugin.
local function discoverPlugins(dir)
local plugins = {}
-- platform-specific dir scan; here a mock example
for _, name in ipairs({"logger", "cache", "auth"}) do
local ok, mod = pcall(require, dir .. "." .. name)
if ok then plugins[name] = mod end
end
return plugins
endUsing package.path for Discovery
Extend package.path so that require can find plugins in a custom directory.
package.path = package.path .. ";./plugins/?.lua"
local plugin = require("myplugin")loadfile for Dynamic Loading
loadfile(path) compiles a Lua file into a chunk without executing it. Execute it with () to get the plugin's module table.
local function loadPlugin(path)
local chunk, err = loadfile(path)
if not chunk then return nil, err end
local ok, result = pcall(chunk)
if not ok then return nil, result end
return result
endPlugin Manifest
Each plugin exports a manifest table describing its name, version, and dependencies.
-- plugins/logger.lua
return {
name = "logger",
version = "1.0",
deps = {},
init = function(app) app:on("log", print) end,
}Dependency Resolution
Before initializing a plugin, check that all its dependencies are already loaded.
local function initPlugin(app, plugins, name)
local p = plugins[name]
if not p then error("Plugin not found: " .. name) end
for _, dep in ipairs(p.deps or {}) do
if not plugins[dep] then
error(("Plugin %s requires %s"):format(name, dep))
end
end
if p.init then p.init(app) end
endHot-Reloading Plugins
To reload a plugin: remove it from package.loaded, reload with require, re-register hooks.
package.loaded["plugins.logger"] = nil
plugins.logger = require("plugins.logger")
plugins.logger.init(app)Plugin Registry
Maintain a registry of loaded plugins to prevent double-loading and enable dependency checking.
Error Isolation
Load plugins with pcall so that a broken plugin does not crash the entire application.
local ok, err = pcall(function()
plugins[name] = loadPlugin(path)
initPlugin(app, plugins, name)
end)
if not ok then
print("Plugin load failed:", name, err)
endPlugin Metadata
Store additional metadata: description, author, config schema. Use it for UI display or auto-configuration.
Lazy Loading
Load plugins only when they are first needed rather than eagerly at startup. Implement via a proxy table with a __index metamethod that triggers loading.
Plugin Loading Question
What is the advantage of using pcall when loading plugins?
Recap: Plugin Discovery and Loading
Discover plugins by scanning directories or using naming conventions. Load with loadfile or require. Validate manifests and resolve dependencies. Always use pcall to isolate load failures.
Frequently asked questions
Is the “Plugin Discovery and Loading” lesson free?
Yes — the full text of “Plugin Discovery and Loading” is free to read here on the web, and the Lua Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Lua Academy course, upgrade to CoddyKit PRO.
What will I learn in “Plugin Discovery and Loading”?
Scan directories for plugin files and load them with require or loadfile. You practise Lua Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Lua Academy?
No prior experience is required. Lua Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Plugin Discovery and Loading” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Lua Academy lesson?
Yes. Every Lua Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Plugin Discovery and Loading
- Plugin API and Hook System
- Sandboxing Plugin Execution
- Versioning and Dependency Management