Object MethodCore Flow

Module.define(name, factory)

Registers a reusable module factory from the macro source, returning true without initializing it yet.

Documented for app version 1.0.50Updated:

Detailed Explanation

This section explains when to use the API, how to call it, and which structures it works best with in production flow.

How To Call It

Module is a global table: call its functions with a dot (Module.<method>(...)), not a colon. Requires Macro Handler 1.0.51 or later.

When To Use It

Registers a reusable module factory from the macro source, returning true without initializing it yet.

Parameters and Return

Names are dotted ASCII identifiers of 1–96 characters; each segment starts with a letter or underscore. A run allows up to 128 definitions and 32 dependency levels. A factory receives its name and must return a table or function. No source string, file or URL is loaded; imported library code must be present in the macro source.

Best Combined With

Resolve dependencies with Module.use inside a factory. Initialization is lazy and successful exports keep their identity for the current run; a new run starts empty. Duplicate definitions and dependency cycles are errors. Failed factories are not cached and can be retried. Use Api.check for requirements; factory bodies remain subject to normal script policy checks.

Example Usage

The snippet below is a starter pattern that can be applied directly in runtime flow.

Module.define("example.normalizer", function()
  return {clean = function(text) return string.lower(text) end}
end)
print(Module.has("example.normalizer"))

Copyable Progressive Examples

From foundation to combined usage, each level is provided as a separate code block so you can copy the level you need and adapt it directly.

Foundation

Shows the shortest direct way to call the API.

Foundation
Module.define("example.normalizer", function()
  return {clean = function(text) return string.lower(text) end}
end)
print(Module.has("example.normalizer"))

Simple

Wraps the base call with minimal flow control.

Simple
local stepOk = true
Module.define("example.normalizer", function()
  return {clean = function(text) return string.lower(text) end}
end)
print(Module.has("example.normalizer"))
if stepOk then
  wait(200)
end

Practical Flow

A practical pattern for real macros with pcall, logging, and guards.

Practical Flow
local ok, err = pcall(function()
  Module.define("example.normalizer", function()
    return {clean = function(text) return string.lower(text) end}
  end)
  print(Module.has("example.normalizer"))
end)

if not ok then
  print("API step failed: Module.define: " .. tostring(err))
  requestStop()
end

Detailed

This level packages the API into a reusable helper with error reporting.

Detailed
-- Module.define(name, factory)
local function run_define_step()
  Module.define("example.normalizer", function()
    return {clean = function(text) return string.lower(text) end}
  end)
  print(Module.has("example.normalizer"))
end

local ok, err = pcall(run_define_step)
if not ok then
  toast("Step failed")
  print(err)
end

Combined

Combines the API with related structures to form a more realistic workflow.

Combined
Module.define("example.normalizer", function()
  return {clean = function(text) return string.lower(text) end}
end)
print(Module.has("example.normalizer"))