Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/154.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 实现菜单系统的lua回调_C++_Lua - Fatal编程技术网

C++ 实现菜单系统的lua回调

C++ 实现菜单系统的lua回调,c++,lua,C++,Lua,在我们的菜单系统中,我们使用用于菜单组件事件回调的lua块在xml中定义菜单。目前,每次调用脚本回调时,我们都调用lua_loadstring,这相当慢。我正试图使它只发生一次,当菜单加载 我最初的想法是为每个菜单组件维护一个lua表,并执行以下操作向表中添加一个新的回调函数: //create lua code that will assign a function to our table std::string callback = "temp." + callbackName + " =

在我们的菜单系统中,我们使用用于菜单组件事件回调的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块将函数分配到表中,而不必使用temp全局变量并运行lua_pcall?

如果要将函数放入表中,请将函数放入表中。看来你的芦苇垛赋不强;考虑一下,

无论如何,我想说你最大的问题是你坚持使用
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);