loading a file iff it exists
Moderator: Forum Moderators
loading a file iff it exists
Does anyone happen to know a technique for loading a file iff it exists? I've tried:
but the "file does not exist" error still propagates and halts my code.
Code: Select all
pcall(wesnoth.require("blah.lua"))
- Alarantalara
- Art Contributor
- Posts: 786
- Joined: April 23rd, 2010, 8:17 pm
- Location: Canada
Re: loading a file iff it exists
pcall doesn't work that way, which is why you're having the problem. It needs a function as an argument to execute, not the result of the execution.
Staying with pcall, the correct way to use it is:
I've used an anonymous function to minimize the space taken, but the point is that you pass the name of a function but do not call it yourself. This means the function must have 0 arguments.
Staying with pcall, the correct way to use it is:
Code: Select all
pcall(function () wesnoth.require("blah.lua") end)
Re: loading a file iff it exists
Hah... of course. Thanks. I swear I read the docs... :-p
Incidentally, though, pcall can take a function with args[1]. This is what I'm doing now (which works):
[1] http://www.lua.org/manual/5.1/manual.html#pdf-pcall
Incidentally, though, pcall can take a function with args[1]. This is what I'm doing now (which works):
Code: Select all
pcall(wesnoth.require, "blah.lua")
- Alarantalara
- Art Contributor
- Posts: 786
- Joined: April 23rd, 2010, 8:17 pm
- Location: Canada
Re: loading a file iff it exists
And now I've learned that pcall has variable argument lists. Excellent.