Page 1 of 1

Problem with pcall() and Object:type()

Posted: Sun Feb 16, 2014 8:09 am
by SadBlobfish
So here's my problem in a nutshell.

When I do this...

Code: Select all

player.image = love.graphics.newImage("sprites/Dodger.png")
error(player.image:type())
I get...
Image
which is just what I'd expect.

But... when I use a protected call...

Code: Select all

player.image = love.graphics.newImage("sprites/Dodger.png")
local try = {pcall(player.image:type())}
error(try[1])
I get...
false
which appears to contradict the previous bit of code.

By my understanding, pcall() only returns 'false' when the function it calls has an error. But, as I showed you, the function produced problem when called earlier. Without going into detail, I use the pcall() function extensively throughout my program as a sort error catching thingy (I don't remember the precise name for it, but that's close enough) to make sure a specific value is either an image or a quad. I need to know why it is returning 'false' instead of the expected 'true'.

Also, note that I use error() to return results because it ignores all preceding code (which is full of errors because of this pcall() issue). It's far more easy to use error() than create a whole new Love2d project JUST so I can use print() in the console. Either way, it gets the point across.

Re: Problem with pcall() and Object:type()

Posted: Sun Feb 16, 2014 8:20 am
by bartbes
That is because pcall calls its first argument. Pcall is defined as follows: if you call pcall(f, ...), it calls f(...) in protected mode. You are giving it a string as a function to call, and this then errors. (You'll see that try[2] is "attempt to call a string value".)

I'm not sure if the code you posted is something you actually want fixed, or just an example, but the fixed version would be

Code: Select all

pcall(player.image.type, player.image)
EDIT: I'd also advise you read the message saying your post was queued for approval, instead of posting it again...

Re: Problem with pcall() and Object:type()

Posted: Mon Feb 17, 2014 6:07 am
by SadBlobfish
bartbes wrote:That is because pcall calls its first argument. Pcall is defined as follows: if you call pcall(f, ...), it calls f(...) in protected mode. You are giving it a string as a function to call, and this then errors. (You'll see that try[2] is "attempt to call a string value".)

I'm not sure if the code you posted is something you actually want fixed, or just an example, but the fixed version would be

Code: Select all

pcall(player.image.type, player.image)
Aha! It worked! Thank you so much. Now I just have about half an hour or so of debugging to do. Yippee! :P
bartbes wrote:EDIT: I'd also advise you read the message saying your post was queued for approval, instead of posting it again...
I apologize for this. After I tried posted this topic for the second time, I noticed the message that indicated that the post had to be approved first. I just clicked the continue button the first time without thinking twice. I wasn't associated with the whole your-post-must-be-approved deal, so I assumed it was just some "your topic has been posted. Conglaturations!" sort of message.

Re: Problem with pcall() and Object:type()

Posted: Mon Feb 17, 2014 7:37 am
by bartbes
Because we had a lot of spam in the past, first posts need approval from moderators, you should be past that process now.