为菜单系统实现lua回调

时间:2011-10-18 19:49:19

标签: c++ lua

在我们的菜单系统中,我们使用lua块定义xml中的菜单,用于菜单组件事件的回调。目前,每次调用脚本回调时,我们都会调用lua_loadstring,这很慢。我正试图这样做,所以这只发生一次,当菜单加载。

我最初的想法是为每个菜单组件维护一个lua表,并执行以下操作以向表中添加新的回调函数:

//create lua code that will assign a function to our table
std::string callback = "temp." + callbackName + " = function (" + params + ")" + luaCode + "end";

//push table onto stack
lua_rawgeti(L, LUA_REGISTRYINDEX, luaTableRef_);

//pop table from stack and set it as value of global "temp"
lua_setglobal(L, "temp");

//push new function onto stack
int error = luaL_loadstring(L, callback.c_str());
if ( error )
{   
    const char* errorMsg = lua_tostring(L, -1);
    Dbg::Printf("error loading the script '%s' : %s\n", callbackName, errorMsg);
    lua_pop(L,1);
    return;
}

//call the lua code to insert the loaded function into the global temp table
if (lua_pcall(L, 0, 0, 0)) 
{
    Dbg::Printf("luascript: error running the script '%s'\n", lua_tostring(L, -1));
    lua_pop(L, 1);
}

//table now has function in it

这看起来很脏。有没有更好的方法允许我直接从lua块分配函数到表,而不必使用临时全局变量并运行lua_pcall?

1 个答案:

答案 0 :(得分:3)

如果要将函数放在表中,请将函数放在表中。看来你的Lua-stack-fu并不强大;考虑studying the manual a bit more closely

无论如何,我说你遇到的最大问题是你对params的坚持。回调函数应该是varadic;他们以...为参数。如果他们想要获得这些值,他们应该使用这样的本地人:

local param1, param2 = ...;

但如果您坚持允许他们指定参数列表,您可以执行以下操作:

std::string luaChunk =
    //The ; is here instead of a \n so that the line numbering
    //won't be broken by the addition of this code.
    "local " + params + " = ...; " +
    luaCode;

lua_checkstack(L, 3);
lua_rawgeti(L, LUA_REGISTRYINDEX, luaTableRef_);
if(lua_isnil(L, -1))
{
    //Create the table if it doesn't already exist.
    lua_newtable(L);

    //Put it in the registry.
    lua_rawseti(L, LUA_REGISTRYINDEX, luaTableRef_);

    //Get it back, since setting it popped it.
    lua_rawgeti(L, LUA_REGISTRYINDEX, luaTableRef_);
}

//The table is on the stack. Now put the key on the stack.
lua_pushlstring(L, callbackName.c_str(), callbackName.size());

//Load up our function.
int error = luaL_loadbuffer(L, luaChunk.c_str(), luaChunk.size(),
    callbackName.c_str());
if( error )
{   
    const char* errorMsg = lua_tostring(L, -1);
    Dbg::Printf("error loading the script '%s' : %s\n", callbackName, errorMsg);
    //Pop the function name and the table.
    lua_pop(L, 2);
    return;
}

//Put the function in the table.
lua_settable(L, -3);

//Remove the table from the stack.
lua_pop(L, 1);