tags:

views:

196

answers:

1

My problem is that I'm having trouble specifying paths for Lua to look in.

For example, in my script I have a require("someScript") line that works perfectly (it is able to use functions from someScript when the script is run standalone.

However, when I run my app, the script fails. I believe this is because Lua is looking in a location relative to the application rather than relative to the script.

Hardcoding the entire path down to the drive isn't an option since people can download the game wherever they like so the highest I can go is the root folder for the game.

We have XML files to load in information on objects. In them, when we specify the script the object uses, we only have to do something like Content/Core/Scripts/someScript.lua where Content is in the same directory as Debug and the app is located inside Debug. If I try putting that (the Content/Core...) in Lua's package.path I get errors when I try to run the script standalone.

I'm really stuck, and am not sure how to solve this. Any help is appreciated. Thanks.

P.S. When I print out the default package.path in the app I see syntax like ;.\?.lua in a sequence like...

;.\?.lua;c:...(long file path)\Debug\?.lua; I assume the ; means the end of the path, but I have no idea what the .\?.lua means. Any Lua file in the directory?

+1  A: 

You can customize the way require loads modules by putting your own loader into the package.loaders table. See here:

http://www.lua.org/manual/5.1/manual.html#pdf-package.loaders

If you want to be sure that things are nicely sandboxed, you'll probably want to remove all the default loaders and replace them with one that does exactly what you want and nothing more. (It will probably be somewhat similar to one of the existing ones, so you can use those as a guide.)

brone
I didn't use this method, but I'm putting it as the answer. I used package.path = package.path .. ";Content/Core/Scripts/?.lua"
Person