Lua Academy · Lesson

Plugin Discovery and Loading

Scan directories for plugin files and load them with require or loadfile.

Lesson 1 of 413 steps

Plugin Discovery and Loading is a free Lua Academy lesson on CoddyKit. This is lesson 1 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Lua Academy learning path, and your progress syncs across the web and the CoddyKit app. The Lua Academy course includes 4 lessons in total.

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
end

Using 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
end

Plugin 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
end

Hot-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)
end

Plugin 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.

Free to start

Learn Lua with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
40
Lessons
159

Frequently Asked Questions

Is the “Plugin Discovery and Loading” lesson free?

Yes — any 3 lessons of the Lua Academy learning path, including “Plugin Discovery and Loading”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Lua Academy course includes 4 lessons in total.

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, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 4.

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

  1. Plugin Discovery and Loading
  2. Plugin API and Hook System
  3. Sandboxing Plugin Execution
  4. Versioning and Dependency Management
← Back to Lua Academy