• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

printDefinition(func) || print function definition

Infernum

Senator
Joined
Feb 14, 2015
Messages
5,643
Solutions
559
Reaction score
3,948
made this while learning debug library for fun
always wondered how to print function definition

usage: printDefinition(Game.convertIpToString)
bd33AS5.png


code:
Code:
function printDefinition(func)
    local info = debug.getinfo(func)
    if (info.what == "C") then
        return print("Input may not be a C function.")
    end
    local count = 0
    local startL, endL = info.linedefined, info.lastlinedefined
    for line in io.lines(info.short_src) do
        count = count + 1
        if (count >= startL) and (count <= endL) then
            -- Fix large indentations by replacing all tab characters with 4 spaces
            local text = line:gsub('%\t', '    ')
            print(text)
          
            -- Break loop once last line is printed
            if (count == endl) then
                break
            end
        end
    end
end
 
Last edited:
You should probably name it differently though as printf in other languages is similar to Lua string.format. Could cause confusions.
 
Back
Top