pcall on newImage Function Returns string 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
User avatar
Helvecta
Party member
Posts: 167
Joined: Wed Sep 26, 2012 6:35 pm

pcall on newImage Function Returns string Type

Post by Helvecta » Mon Jun 29, 2015 9:05 pm

Been doing some testing on 0.9.2 and it seems that pcall on the newImage function returns a string :(

Tasty example:

Code: Select all

function love.load()
   meow = love.graphics.newImage("test.png")
   error(type(meow))
end
returns:

Code: Select all

main.lua:3: userdata
but then:

Code: Select all

function love.load()
   err, meow = pcall(love.graphics.newImage("test.png"))
   error(type(meow))
end
returns:

Code: Select all

main.lua:3: string
Using :type() raises more questions. Without pcall, meow:type() returns "Image", but with pcall meow:type() returns "attempt to call method 'type' (a nil value)". I want to use pcall to check if an image cannot be decoded, if anyone's curious (I thought that would be a good way to check if an image is truncated/malformed). Any ideas why this is happening?
"Bump." -CMFIend420

User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: pcall on newImage Function Returns string Type

Post by bartbes » Mon Jun 29, 2015 9:37 pm

You should read that string, it will tell you you're trying to call a userdata object, and err (which isn't the error, rather a flag) will be false.
Now, the reason why this is happening is that you're using pcall wrong. You see, like most things in lua, pcall isn't some magic, it's just a function, if you give it the result of a call.. well, it can only do things with the result, not the call. So instead:

Code: Select all

success, meow = pcall(love.graphics.newImage, "test.png")
And note that if success is false, meow will contain the error message.

User avatar
Helvecta
Party member
Posts: 167
Joined: Wed Sep 26, 2012 6:35 pm

Re: pcall on newImage Function Returns string Type

Post by Helvecta » Mon Jun 29, 2015 11:09 pm

Oh, that's embarrassing, I'm a dumby.. I forgot that pcall functions that way. I know that "err" is a boolean, though, but it flows better than "isNotError". Thanks for letting me know, though! And sorry for wasting your time. :)
"Bump." -CMFIend420

Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 32 guests