私はグーグルのハイとローを持っていて、例を見つけましたが、どれもうまくいかないようです(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
。