Skip to content

Instantly share code, notes, and snippets.

@Silverfeelin
Last active April 27, 2019 14:23
Show Gist options
  • Save Silverfeelin/f3b98f1c88e80b24bc3f43b82dc68a0d to your computer and use it in GitHub Desktop.
Save Silverfeelin/f3b98f1c88e80b24bc3f43b82dc68a0d to your computer and use it in GitHub Desktop.
Starbound include

Defining Modules

A module simply exports variables to a global module.

If your module includes other modules, make sure you include them before exporting.

/scripts/shapes.lua

local shapes = {}

function shapes.circle(...)
  -- Do stuff.
end

-- This statement will export the contents of 'shapes' as a module.
module = shapes

Using Modules

require "/scripts/include.lua"

-- Local reference to module
local shapes = include("/scripts/shapes.lua")
--- shapes.circle(...)

-- Global reference to module
include("/scripts/dyes.lua", "dyes")
--- dyes.red
local modules = {}
local req = function(script)
local old, m
old, module = module, nil
require(script)
m, module = module, old
modules[script] = m
return m
end
--- Loads a module
-- @param script Absolute path to script file
-- @param [global] If present, exposes the module under _ENV[global].
-- @returns exported module
function include(script, global)
local m = _SBLOADED[script] and modules[script] or req(script)
if global and not _ENV[global] then _ENV[global] = m end
return m
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment