Gentle Error Handling - play sound effect when image absent

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
Snackrilege
Prole
Posts: 19
Joined: Sat Sep 06, 2014 8:50 pm

Gentle Error Handling - play sound effect when image absent

Post 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)
Last edited by Snackrilege on Tue Sep 09, 2014 8:27 pm, edited 2 times in total.
User avatar
slime
Solid Snayke
Posts: 3160
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

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

Post 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.
Snackrilege
Prole
Posts: 19
Joined: Sat Sep 06, 2014 8:50 pm

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

Post 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.
User avatar
slime
Solid Snayke
Posts: 3160
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

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

Post 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
Snackrilege
Prole
Posts: 19
Joined: Sat Sep 06, 2014 8:50 pm

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

Post by Snackrilege »

yep. they sure did get me with that gotcha. Way to diagnose, Slime.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest