Page 1 of 1

[SOLVED] `pcall(...)` inside `Canvas:renderTo(...)`

Posted: Sun Apr 29, 2018 7:42 am
by nikki93
Hey all!

I'm trying to do a `pcall(...)` inside a `Canvas:renderTo(...)`, which looks as follows (with the `print(...)` calls added for debugging):

Code: Select all

function renderTo(func)
    if canvas then
        canvas:renderTo(function()
            print('before')
            pcall(func)
            print('after')
        end)
    end
end
The hope here is to allow other code to do `renderTo(function() ... end)` with possibly an error occurring in the `...`.

It works fine when there is no error raised by `func`, but when it does raise an error, I get:

Code: Select all

Error: [string "boot.lua"]:514: present cannot be called while a Canvas is active.
I can confirm that both 'before' and 'after' are successfully printed before this error occurs. Wondering if there is something I may be missing here. 🤔

FWIW, I am on 11.0, will try upgrading to 11.1 soon.

Re: `pcall(...)` inside `Canvas:renderTo(...)`

Posted: Sun Apr 29, 2018 8:36 am
by zorg
At first glance, i'd say that pcall caught the error, allowed code execution to continue (since that's what it's for), but the codepath didn't close normally, meaning the canvas was still active because renderTo didn't finish execution, hence it didn't set the active framebuffer back to the screen... and love.graphics.present in love.run can only be called if the active "canvas" is the main framebuffer.

That said, looking at the source code of Canvas:renderTo, it does its own pcall as well... so it shouldn't have issues with not setting back the previous canvas(es)?

Re: `pcall(...)` inside `Canvas:renderTo(...)`

Posted: Sun Apr 29, 2018 9:31 am
by nikki93
@zorg: Thanks for the response! I figured it out I think -- it was due to a `love.graphics.push('all')` being unmatched by a `love.graphics.pop()` inside `func`. I'll probs end up using / creating a utility like https://github.com/djfdyuruiry/lua-try-catch-finally to make this all nicer to write.

Re: `pcall(...)` inside `Canvas:renderTo(...)`

Posted: Tue Aug 14, 2018 9:56 am
by NetherGranite
nikki93 wrote: Sun Apr 29, 2018 9:31 am it was due to a `love.graphics.push('all')` being unmatched by a `love.graphics.pop()` inside `func`.
Thanks for sharing, I got the exact same error, and your solution to your problem lead me to realize that I failed to match a

Code: Select all

love.graphics.setCanvas(canvas)
with a

Code: Select all

love.graphics.setCanvas()
to reset it. Why did we get such a strange error message? I wasn't able to find this error message anywhere else but this thread, but surely others have forgotten to follow a state change up with a respective reset, right?

Re: [SOLVED] `pcall(...)` inside `Canvas:renderTo(...)`

Posted: Tue Aug 14, 2018 6:34 pm
by pgimeno
love.graphics.present() is called by the default love.run. If a canvas is active when love.draw finishes, you'll get that error because that's when love.graphics.present is called. That's the 'present' that the error message refers to.