Page 1 of 1

Gentle Error Handling - play sound effect when image absent

Posted: Tue Sep 09, 2014 8:06 pm
by Snackrilege
[SOLVED]

in this particular case, the answer was simply to test

Code: Select all

if not love.filesystem.isFile(pathOfPotentialImage) then --[[load generic instead and play warning sound effect]] end

-----------------------------------------------------
-----------------------------------------------------
[/b]




I'm not used to working with scripting languages, so this is really difficult for me to wrap my head around conceptually.

We have built a very simple tile-based level editor for our project that is itself a love2d program. We load in tiles by changing a few parameters in a config file, then pressing "L" to load the settings. very simple.

The big problem is that if we screw up one of the parameters in the config file, it crashes the editor!

For instance, if I enter in the config file the following slip up:

Code: Select all

image_filename='dessert_maps/sand_tile'
notice -- i spelled desert wrong there, the actual filename should be 'desert_maps/sand_tile'

save the file, return to the editor and press "L' to load settings, the editor will crash, and I will lose what I've been working on.

I'd prefer if it simply did nothing, or loaded a default image, and then played an angry sound effect to let me know I need to try again.

Unfortunately, the typical "try, catch, finally" approach doesn't really pan out in lua. Or maybe it just plain doesn't work in a scripting language. I don't know.


Any thoughts on how I might correct this? (OTHER than making a better editor that doesn't relly on manually editing a config file. I know, I know, okay? But this represents a larger problem that would be nice to be get straightened out)

Re: Gentle Error Handling - play sound effect when image abs

Posted: Tue Sep 09, 2014 8:09 pm
by slime
You can use [wiki]love.filesystem.isFile[/wiki] to check if there's an actual file at a filepath. Or you can use pcall when calling love.graphics.newImage, although the isFile method would probably be nicer.

Re: Gentle Error Handling - play sound effect when image abs

Posted: Tue Sep 09, 2014 8:18 pm
by Snackrilege
the latter is what I had attempted, but I didn't get any success.

with the pcall, it would still end the program immediately upon calling the bad love.graphics.newImage() call, even when I tried redefining the love.errhand(msg) function

I'm not sure if I made a syntactical error or if there's something I just don't quite understand about pcalls and error handling.

I am definitely going to immediately investigate "isFile" though. Thanks!


update: isFile is perfect for this application. Works gracefully and precisely as I want. Thanks so much. Hopefully I'll eventually figure out pcall aswell, but this will work just fine in the interim.

Re: Gentle Error Handling - play sound effect when image abs

Posted: Tue Sep 09, 2014 8:33 pm
by slime
One 'gotcha' with pcall is that you have to pass the actual newImage function as an argument, rather than calling newImage and passing the result in.

For example:

Code: Select all

local status, image = pcall(love.graphics.newImage, filepath)
if status then
    -- file exists and was loaded into an image object.
else
    -- file doesn't exist or wasn't able to be loaded into an image object.
end

Re: Gentle Error Handling - play sound effect when image abs

Posted: Tue Sep 09, 2014 9:14 pm
by Snackrilege
yep. they sure did get me with that gotcha. Way to diagnose, Slime.