메뉴 시스템에서는 메뉴 구성 요소 이벤트에 대한 콜백에 사용되는 루아 청크가있는 xml 메뉴를 정의합니다. 현재, 스크립트 콜백이 호출 될 때마다 우리는 매우 느린 lua_loadstring을 호출합니다. 메뉴를로드 할 때이 일이 한 번만 수행되도록하려면 노력하고있어.메뉴 시스템에 대한 루아 콜백 구현
//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