ReferenceDebug

debug.setconstant

Replaces an existing constant in a Lua function.

Syntax

debug.setconstant(target: function | number, index: number, value: any) -> ()

Also available as

setconstantset_constant

Parameters

ParameterTypeDescription
targetfunction or numberLua function or visible caller level.
indexnumberOne-based constant slot.
valueanyReplacement compatible with the slot.

Description

Choose the index with debug.getconstants. Cached table, property and method lookups that use the changed constant are invalidated, including warmed userdata accesses. The operation changes compiled literal data, not an upvalue or stack local. The replacement must satisfy the function’s constant-type restrictions. A C function, invalid slot or incompatible value raises an error. Keep the original value if the change needs to be reversed.

Example

local function sample() return "before" end
for index, value in debug.getconstants(sample) do
    if value == "before" then
        debug.setconstant(sample, index, "after")
    end
end
print(sample())

These are Game executor contracts. Check build requirements and session lifetimes before using them.