lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hello,

I am having a problem with including other Lua files from a Lua file because of inconsistent paths. Here's are the three files that I have:
---- main.cc ---
{
...
	luaL_loadfile(L, "subdir/hello.lua");
	lua_pcall(L, 0, LUA_MULTRET, 0);
...
}
---- subdir/hello.lua ---
...
	dofile('subhello.lua');
...
---- subdir/subhello.lua ---
...
	print('Hello world');
...
---- -----

However, this fails with the message:
cannot read subhello.lua: No such file or directory

I have trolled the web for an answer, but I can't find anything. It seems there are three ways to include a lua file
* dofile
* loadfile
* require

However, none is relative to the current Lua source file. Of course, I could do a dofile('subdir/subhello.lua'); but that doesn't help me, because the name of 'subdir' is not known until runtime.

Is there any way to get around this? A workaround would be to get the current's file path and concatenate the strings, but I haven't found a way to do that either.

Thanks in advance,
Tony