Lua: making require work as one might expect

Posted by Worstje on Tue 16 Sep 2008 07:36 PM — 3 posts, 11,406 views.

Netherlands #0
For a while I've been bothered by Lua. When I require something, I generally want it to check the directory with my plugin.xml first, and other directories at a later stage. The 'current directory' is rather useless due to the fact that it changes depending on user activity and can't garantuee any relationship to the files you are including. I was finally bothered enough to find out how to solve this little annoyance of mine, and hopefully other people will find it useful too.

-- Adjusted to match WillFa's improvement comment below.

plugin_path = string.match(world.GetPluginInfo(world.GetPluginID(), 6), "(.*\\).*$")
package.path = plugin_path .. "?;" .. plugin_path .. "?.lua;" .. package.path

require "something"

Amended on Wed 17 Sep 2008 05:46 AM by Worstje
USA #1
A RegExp of "^(.+\\).-$" should cleaner and not require the concatenation op.


Thanks! I was irked by this too and never gave the thought on how to fix it. :)
Netherlands #2
Bah, just noticed the forums unescaped my backslashes. I'll try to fix that in my original post in a moment.

And yeah, true. Don't know why I didn't do the regular expression like that - probably because I figured Lua's expressions wouldn't like it for some reason. Thanks for minor improvement. ^_^