Libraries - tayjay/SCriPt GitHub Wiki
As your scripts grow you'll want to share helper functions and data between them instead of copy-pasting. SCriPt supports Lua's standard require for exactly this, scoped to a dedicated library folder.
Shared library modules live in:
LabAPI/SCriPt/Scripts/lib/
Files in lib/ are not run as scripts on their own (unlike files in global/ or a <port>/ folder). They only load when another script requires them.
A library module is a normal Lua file that returns a value (usually a table of functions). For example, create Scripts/lib/util.lua:
local util = {}
function util.greet(player)
player.SendBroadcast("Welcome, " .. player.DisplayName .. "!", 5)
end
function util.count_scps()
return #Players.ByTeam(Team.SCPs)
end
return utilIn any script in global/ or <port>/, call require with the module name (no .lua, no path):
local util = require("util") -- loads Scripts/lib/util.lua
greeter = SCriPt.Module('Greeter')
function greeter.load()
greeter.on(Events.Player.Joined, greeter.onJoined)
end
function greeter.onJoined(args)
util.greet(args.Player)
endrequire("name") looks for, in order:
require(...) |
Resolves to |
|---|---|
require("util") |
lib/util.lua |
require("util") |
lib/util/init.lua |
require("combat.aim") |
lib/combat/aim.lua (a . becomes a folder separator) |
The result is cached — requiring the same module again returns the same value without re-running the file, so modules can safely hold shared state.
-
requireis available to scripts even while sandboxed. - For safety, resolution is locked to the
lib/folder — you cannotrequirea path outside it (no..traversal, no absolute paths). - The broader loaders (
dofile,loadfile,load) are disabled while sandboxed; onlyrequireis available. They are re-enabled with thefull_accessconfig option (see Dangerous).