Volt

debug.getproto

Gets a proto (nested function) from a function.

Syntax

debug.getproto(func: function | number, index: number, activated?: boolean) -> function | table

Parameters

ParameterTypeDescription
funcfunction or numberThe function or stack level
indexnumberThe proto index (1-based)
activatedboolean?If true, returns all activated instances

Returns

TypeDescription
functionThe proto at the specified index (if activated is false/nil)
tableArray of activated instances (if activated is true)

Description

debug.getproto retrieves a nested function definition (proto) from within a function. Protos are functions defined inside other functions.

Example

local function outer()
    local function inner1()
        print("inner1")
    end
    
    local function inner2()
        print("inner2")
    end
    
    inner1()
    inner2()
end

-- Get the first nested function
local proto1 = debug.getproto(outer, 1)
proto1() -- Output: inner1

-- Get the second nested function
local proto2 = debug.getproto(outer, 2)
proto2() -- Output: inner2

Activated Instances

When activated is true, returns all instances of the proto that have been created:

local instances = {}

local function factory()
    local function create()
        return {}
    end
    table.insert(instances, create())
    table.insert(instances, create())
end

factory()

-- Get all activated instances of the inner function
local activated = debug.getproto(factory, 1, true)
print(#activated) -- Number of times the inner function was instantiated

On this page