lua-users home
lua-l archive

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


In Lua 5.1, I'm running into problems calling luaL_loadfile() (followed by
lua_pcall()) from a C function that's called from a script.  The Lua
function is a __index metamethod, but it's not clear to me why that makes
any difference.  When I call the loaded chunk, Lua returns an error that
referenced global variables are nil as if it can't see any globals at all.

Essentially the code looks like:

Lua:

function auto(t, key)
   ...
   class = SEGetLuaClass(classpath)
   return class
end

table = setmetatable({}, {__index = auto})

C:

int SEGetLuaClass(lua_State *L)
{
    /* Turn the argument into a file name path... */

    luaL_loadfile(L, sScriptPath);
    lua_pcall(L, 0, 0, 0);
}

The error returned is something like:

../../scripts\Classes\UI\Objects\Texture.lua:9: attempt to call global 'class' (a nil value)

even though class has been defined.  Even if I explicitly use _G.class in
Texture.lua, I get the same error.

If I call SEGetLuaClass() directly from Lua, it all works like it should.

Are you not allowed to load or run chunks in metamethods?