Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Is it possible to find the current line of code you're on?

Asked by
LuaQuest 450 Moderation Voter
8 years ago

Basically I'm just wondering if it's possible for a script to find out what line of code it's on. For example, say you wanted to make a custom error handler, and have a function return (as a string value), what line of code the error was on. How would this be done? Is this even possible?

Here's what I made for my error handler so far, but it's kinda useless since it's just different ways to use the error function:

local function errorCheck(v,err)
    if not v then
        error("Error: "..tostring(err))
    end
    return true
end

print(errorCheck(nil,"the value is nil")) -- pretty much just the assert function

But instead of the script breaking, I'd want it to only return the line that the error is on, and that's it.

P.S:

LoadStringEnabled is disabled, and I don't want to use loadstring for this.

Simplified question:

How do I find the line of code a function call is on?

0
I don't know any efficient methods, but you could redefine a variable at the beginning of every line equivalent to the line it's on, and print that on the error handler. aquathorn321 858 — 8y
0
yeah but masta, that only fires if the code has already broken. i'm trying to find a way to get the current line of where ever i'm calling the function LuaQuest 450 — 8y

3 answers

Log in to vote
1
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
8 years ago
Edited 7 years ago

debug.traceback() is a function that provides the current call-stack (as a string).

Thus print( debug.traceback() ) will make something that looks fairly like an error message, where the first line is the current line, and the second is the caller, etc.

Here's a simple function which cleans up the trace. Warning: this will fail on many things, it makes a lot of assumptions.

function trace()
    local msg = debug.traceback()
    local t = {}
    for x in msg:gmatch("[^\n]+") do
        local source, line, message = x:match("^(.+):(%d+):%s+(.+)$")
        if message then
            table.insert(t, {source = source, line = line * 1, message = message})
        end
    end
    table.remove(t, 1) -- hide trace implementation
    return t
end

Keep in mind that scripts can have :s in their names, messages could have colons and newlines (maybe?), and that tail-recursive1 calls get weird output that this will ignore


  1. EDIT ROBLOX does not support tail calls, so this can be disregarded 

Ad
Log in to vote
0
Answered by 8 years ago

If you pause the test in Studio, and you check the script, it should have a line in it, unless it is completely carried out, highlighted in yellow with a yellow pointer.

Hope this helped ;)

0
I know how to debug a program e.e... My question is asking how to have a function return the line of code it's on. LuaQuest 450 — 8y
Log in to vote
-1
Answered by 8 years ago

I think this might be what you are looking for.


game:GetService("ScriptContext").Error:connect(function(message, trace, script) print(script:GetFullName().." errored!") print("Reason: "..message) print("Trace: "..trace) end


0
This is for something that's already thrown an error. I'm trying to find the current line of code of a function call. LuaQuest 450 — 8y

Answer this question