Home

Awesome

Lua: Require Extended

This is a small module that replaces the require function in Lua. It extends its behavior and allows you to load a file with inputs and receive multiple outputs.

Example:

local output1, output2 = require ("libs.myFile", input1, input2, input3)

Inside the Required File:

To access the input values inside a required file, use the same methods you'd use for a function with varargs. To return multiple values from a required file, simply return multiple values (easy, right?).

-- (Inside libs/myFile.lua)
-- (The first input is always the require path)
local requirePath, param2, param3 = ...

-- *** Do something within the file ***

return "foo", "bar"

Including the Module:

Simply download this file and require it at the top of your program like so:

require ("requireExt")

Notes:

Potential Uses:

-- (Inside camera/init.lua)
local requirePath = ...
local camera = {}
require (requirePath .. ".utils", camera) -- Loads the submodule
return camera
-- (Inside camera/utils.lua)
local requirePath, camera = ...
camera.utils = {}
-- *** A list of functions for the submodule ***

local blankMap = {0, 0, 0}; local mapManager = require ("mapManager", blankMap)

local useSlices = false; local menuMaster = require ("menuMaster", useSlices)

local version = "Android"; local playerCon = require ("playerController", version)

local screenHandler, message = require ("classes.screenHandler")

Known Issues:

Final Thoughts:

Admittedly, some might argue that this module is redundant and might encourage people to create messier code. To that, I must agree. Generally, there are many other ways to accomplish the potential uses I described. However, I've uploaded it here in case someone still finds this useful for their personal programming style. After all, I think it's important to offer alternative methods.