Attempt to call upvalue "error_result" from xpcall

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
saltyspaghetto
Prole
Posts: 1
Joined: Sat Apr 08, 2023 4:37 pm

Attempt to call upvalue "error_result" from xpcall

Post by saltyspaghetto »

Hello LOVE2D forum! This is my first time on here so I am very sorry for any mistakes I make.
I am making a small game and keep getting this error when trying to make an error system.

main.lua:105: attempt to call upvalue 'error_result' (a string value)
[love "callbacks.lua"]:228: in function 'handler'
main.lua:105: in function <main.lua:103>
[C]: in function 'xpcall'


I am not quite sure what to do, it seems to work in another game I have been tweaking/editing.
I will post the code here.

code that causes the error inside of main.lua

Code: Select all

return function()
        if error_result then
            local result = error_result()
            if result then
                if result == "reload" then
                    Mod = nil
                    error_result = nil
                else
                    if love.quit then
                        love.quit()
                    end
                    return result
                end
            end
        else
            local success, result = xpcall(mainLoop, Leggle.errorHandler)
            if success then
                return result
            else
                error_result = result
            end
        end
    end
and here is the Leggle.errorHandler code.
local function error_printer(msg, layer)
print((debug.traceback("Error: " .. tostring(msg), 1+(layer or 1)):gsub("\n[^\n]+$", "")))
end

Code: Select all

function Leggle.errorHandler(msg)
    local copy_color = {1, 1, 1, 1}
    local anim_index = 1
    --local font = love.graphics.newFont("assets/fonts/main.ttf", 32, "mono")
    --local smaller_font = love.graphics.newFont("assets/fonts/main.ttf", 16, "mono")

    msg = tostring(msg or "nil")

    error_printer(msg, 2)

    if not love.window or not love.graphics or not love.event then
        return
    end

    local width  = SCREEN_WIDTH
    local height = SCREEN_HEIGHT

    local window_scale = 1

    if Leggle.Config then
        window_scale = Leggle.Config["windowScale"] or 1
        if window_scale ~= 1 then
            local width  = SCREEN_WIDTH  * window_scale
            local height = SCREEN_HEIGHT * window_scale
        end
    end

    if not love.window.isOpen() then
        local success, status = pcall(love.window.setMode, width, height)
        if not success or not status then
            return
        end
    end

    -- Reset state.
    if Input then Input.clear(nil, true) end
    if love.mouse then
        love.mouse.setVisible(true)
        love.mouse.setGrabbed(false)
        love.mouse.setRelativeMode(false)
        if love.mouse.isCursorSupported() then
            love.mouse.setCursor()
        end
    end
    if love.joystick then
        -- Stop all joystick vibrations.
        for i,v in ipairs(love.joystick.getJoysticks()) do
            v:setVibration()
        end
    end
    if love.audio then love.audio.stop() end

    love.graphics.reset()

    love.graphics.setColor(1, 1, 1, 1)

    local trace = debug.traceback("", 2)

    love.graphics.origin()

    local split = Utils.split(msg, ": ")

    local function draw()

        local pos = 32
        local ypos = pos
        love.graphics.origin()
        love.graphics.clear(0, 0, 0, 1)
        love.graphics.scale(window_scale)

        love.graphics.setColor(1, 1, 1, 1)
        love.graphics.setFont(smaller_font)
        love.graphics.printf("Leggle v" .. tostring(Leggle.Version), -20, 10, 640, "right")

        love.graphics.setFont(font)

        local _,lines = font:getWrap("Error at "..split[#split-1].." - "..split[#split], 640 - pos)

        love.graphics.printf({"Error at ", {0.6, 0.6, 0.6, 1}, split[#split-1], {1, 1, 1, 1}, " - " .. split[#split]}, pos, ypos, 640 - pos)
        ypos = ypos + (32 * #lines)

        for l in trace:gmatch("(.-)\n") do
            if not l:match("boot.lua") then
                if l:match("stack traceback:") then
                    love.graphics.setFont(font)
                    love.graphics.printf("Traceback:", pos, ypos, 640 - pos)
                    ypos = ypos + 32
                else
                    love.graphics.setFont(smaller_font)
                    love.graphics.printf(l, pos, ypos, 640 - pos)
                    ypos = ypos + 16
                end
            end
        end

        -- DT shouldnt exceed 30FPS
        DT = math.min(love.timer.getDelta(), 1/30)

        copy_color[1] = copy_color[1] + (DT * 2)
        copy_color[3] = copy_color[3] + (DT * 2)

        --love.graphics.setFont(smaller_font)
        love.graphics.setColor(1, 1, 1, 1)
        love.graphics.print("Press ESC to restart the game", 8, 480 - 40)
        love.graphics.setColor(copy_color)
        love.graphics.print("Press CTRL+C to copy traceback to clipboard", 8, 480 - 20)
        love.graphics.setColor(1, 1, 1, 1)

        love.graphics.present()
    end

    local function copyToClipboard()
        if not love.system then return end
        copy_color = {0, 1, 0, 1}
        love.system.setClipboardText(trace)
        draw()
    end

    return function()
        if love.graphics.isActive() and love.graphics.getCanvas() then
            love.graphics.setCanvas()
        end

        love.event.pump()

        for e, a, b, c in love.event.poll() do
            if e == "quit" then
                return 1
            elseif e == "keypressed" and a == "escape" then
                return "reload"
            elseif e == "keypressed" and a == "c" and Input.ctrl() then
                copyToClipboard()
            elseif e == "touchpressed" then
                local name = love.window.getTitle()
                if #name == 0 or name == "Untitled" then name = "Game" end
                local buttons = {"OK", "Cancel"}
                if love.system then
                    buttons[3] = "Copy to clipboard"
                end
                local pressed = love.window.showMessageBox("Quit "..name.."?", "", buttons)
                if pressed == 1 then
                    return 1
                elseif pressed == 3 then
                    copyToClipboard()
                end
            end
        end

        if love.timer then
            DT = love.timer.step()
        end

        draw()

        love.timer.sleep(0.01)
    end

end
sorry for such a long forum post. Thank you in advance.
:cool:
User avatar
Bigfoot71
Party member
Posts: 287
Joined: Fri Mar 11, 2022 11:07 am

Re: Attempt to call upvalue "error_result" from xpcall

Post by Bigfoot71 »

At a glance what I find strange is that in your first piece of code line 20 there is:

Code: Select all

error_result = result
Result which is supposed to be a character string precisely if I understand correctly, and at the beginning of this same example you do:

Code: Select all

local result = error_result()
So since we don't have the full code to test I can only assume that error_result is redefined with the string `result` and you try to call it again afterwards.

Edit: And welcome to you by the way :D
My avatar code for the curious :D V1, V2, V3.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot], Google [Bot], Semrush [Bot] and 4 guests