Engine
engine.load_string
Shared
Executes a buffer within the engine
Syntax
local result = engine.load_string(buffer, chunk_name = "", auto_load = true, use_env = false, env = nil)Parameters
| Type | Name | Description |
|---|---|---|
string | buffer | Buffer containing code to be loaded and executed |
string | chunk_name | Label used in error messages to identify the source Example: "scripts/init.lua" |
bool | auto_load | Determines execution behavior: • When true - the loaded function executes immediately• When false - returns a callable function for deferred execution |
bool | use_env | Determines environment behavior: • When true - executes the buffer inside the provided env table• When false - executes the buffer in the default global environment |
table | env | A table to use as the execution environment. Reads and writes inside the buffer are scoped to this table. Typically used with __index = _G to retain access to globals |
Returns
| Type | Name | Description |
|---|---|---|
function / bool | result | Value depends on the auto_load argument:• When auto_load is false — the loaded function on success, or false on failure• When auto_load is true — true on successful execution, or false on failure |
Examples
local result = engine.load_string([[
engine.print("info", "Immediate", "Execution")
]], nil, true)local exec = engine.load_string([[
engine.print("info", "Delayed", "Execution")
]], nil, false)
if exec then
exec() --Execute the loaded function manually
endlocal result = engine.load_string(
"local x =",
"scripts/init.lua"
) --Error: scripts/init.lua:2: unexpected symbol near '<eof>'local env = setmetatable({}, {__index = _G})
engine.load_string([[
foo = "hello"
]], nil, true, true, env)
engine.load_string([[
engine.print("info", foo) --prints "hello"
]], nil, true, true, env)