Vital.sandbox
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

TypeNameDescription
stringbufferBuffer containing code to be loaded and executed
stringchunk_nameLabel used in error messages to identify the source
Example: "scripts/init.lua"
boolauto_loadDetermines execution behavior:
• When true - the loaded function executes immediately
• When false - returns a callable function for deferred execution
booluse_envDetermines environment behavior:
• When true - executes the buffer inside the provided env table
• When false - executes the buffer in the default global environment
tableenvA 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

TypeNameDescription
function / boolresultValue depends on the auto_load argument:
• When auto_load is false — the loaded function on success, or false on failure
• When auto_load is truetrue on successful execution, or false on failure

Examples

Immediate execution with auto_load enabled
local result = engine.load_string([[
    engine.print("info", "Immediate", "Execution")
]], nil, true)
Deferred execution with auto_load disabled
local exec = engine.load_string([[
    engine.print("info", "Delayed", "Execution")
]], nil, false)

if exec then
    exec() --Execute the loaded function manually
end
Use chunk_name to get readable error locations
local result = engine.load_string(
    "local x =", 
    "scripts/init.lua"
) --Error: scripts/init.lua:2: unexpected symbol near '<eof>'
Persistent environment across multiple load_string calls
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)

On this page