Max*_*and 5 c++ lua lua-5.2 c++builder-xe3
我有谷歌的高低,并找到了例子,但似乎没有一个工作(Lua 5.2).
我在Lua中有一个简单的功能
function onData ( data )
  print ( data )
end
我想onData从C++ 调用并尝试这个:
// Create new Lua state
L = luaL_newstate();
// Load all Lua libraries
luaL_openlibs(L);
// Create co-routine
CO = lua_newthread(L);
// Load and compile script
AnsiString script(Frame->Script_Edit->Text);
if (luaL_loadbuffer(CO,script.c_str(),script.Length(),AnsiString(Name).c_str()) == LUA_OK) {
  Compiled = true;
} else {
  cs_error(CO, "Compiler error: ");    // Print compiler error
  Compiled = false;
}
// Script compiled and ready?
if (Compiled == true) {
  lua_getglobal(CO, "onData");    // <-------- Doesn't find the function
  if( !lua_isfunction(CO,-1)) {
    lua_pop(CO,1);
    return;
  }
  lua_pushlstring(CO,data,len);
  lua_resume(CO,NULL,0)
}
正如您所看到的,我正在开始我的脚本作为一个例程,所以我可以使用lua_yield()它上面的函数.我试图在状态L和CO状态中寻找功能.
luaL_loadbuffer加载脚本但不运行它。onData仅在脚本运行时定义。
尝试调用luaL_dostring而不是luaL_loadbuffer.
或者加lua_pcall(CO,0,0,0)在前面lua_getglobal。
此外,您需要lua_resume(CO,NULL,1)传递data到onData.
| 归档时间: | 
 | 
| 查看次数: | 3047 次 | 
| 最近记录: |