[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: [Newbie] Including a lua file from another lua file (problems with paths)
- From: Jerome Vuarand <jerome.vuarand@...>
- Date: Thu, 16 Jul 2009 14:59:20 +0200
2009/7/16 Antoine Bruguier <Antoine.Bruguier@brion.com>:
> 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.
Try modules and require:
---- main.cc ---
{
...
lua_getglobal(L, "require");
lua_pushstring(L, "subdir.hello");
lua_pcall(L, 1, LUA_MULTRET, 0);
...
}
---- subdir/hello.lua ---
...
module(..., package.seeall)
require(_PACKAGE..'subhello');
...
---- subdir/subhello.lua ---
...
print('Hello world');
...
---- -----