メニューシステムでは、メニューコンポーネントイベントのコールバックに使用される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_pcallを実行することなく、関数をluaチャンクから直接テーブルに割り当てることができるより良い方法はありますか?
感謝それをクリアする。私は "..."を使用します – brunoma