Problem with pcall() and Object:type()

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
SadBlobfish
Prole
Posts: 6
Joined: Wed Nov 27, 2013 4:30 am

Problem with pcall() and Object:type()

Post 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.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

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

Post 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...
SadBlobfish
Prole
Posts: 6
Joined: Wed Nov 27, 2013 4:30 am

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

Post 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.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

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

Post 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.
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 2 guests