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

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
User avatar
nikki93
Prole
Posts: 12
Joined: Mon Mar 19, 2018 9:52 am

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

Post by nikki93 » Sun Apr 29, 2018 7:42 am

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.
Last edited by nikki93 on Sun Apr 29, 2018 9:32 am, edited 1 time in total.

User avatar
zorg
Party member
Posts: 2413
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

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

Post by zorg » Sun Apr 29, 2018 8:36 am

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)?
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.

User avatar
nikki93
Prole
Posts: 12
Joined: Mon Mar 19, 2018 9:52 am

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

Post by nikki93 » Sun Apr 29, 2018 9:31 am

@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.

NetherGranite
Prole
Posts: 22
Joined: Wed Jul 11, 2018 11:08 pm

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

Post by NetherGranite » Tue Aug 14, 2018 9:56 am

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?

User avatar
pgimeno
Party member
Posts: 1345
Joined: Sun Oct 18, 2015 2:58 pm

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

Post by pgimeno » Tue Aug 14, 2018 6:34 pm

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.
Thrust II Reloaded - GifLoad for Löve - GSpöt GUI - My NotABug.org repositories - portland (mobile orientation)
The MS-Github repositories I had have been closed after the acquisition announcement and will be removed in the near future.

Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests